-
-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Generate full client test suite and improve typed client (#…
- Loading branch information
Showing
25 changed files
with
27,395 additions
and
11,081 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { generator, toFile } from '@feathershq/pinion' | ||
import { renderSource } from '../../commons' | ||
import { AppGeneratorContext } from '../index' | ||
|
||
const template = ({ lib }: AppGeneratorContext) => /* ts */ `import assert from 'assert' | ||
import axios from 'axios' | ||
import type { Server } from 'http' | ||
import { app } from '../${lib}/app' | ||
import { createClient } from '../${lib}/client' | ||
import rest from '@feathersjs/rest-client' | ||
const port = app.get('port') | ||
const appUrl = \`http://\${app.get('host')}:\${port}\` | ||
describe('client tests', () => { | ||
const client = createClient(rest(appUrl).axios(axios)) | ||
it('initialized the client', () => { | ||
assert.ok(client) | ||
}) | ||
}) | ||
` | ||
|
||
export const generate = (ctx: AppGeneratorContext) => | ||
generator(ctx).then(renderSource(template, toFile('test', 'client.test'))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/cli/src/authentication/templates/client.test.tpl.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { generator, toFile } from '@feathershq/pinion' | ||
import { renderSource } from '../../commons' | ||
import { AuthenticationGeneratorContext } from '../index' | ||
|
||
const template = ({ | ||
authStrategies, | ||
upperName, | ||
type, | ||
lib | ||
}: AuthenticationGeneratorContext) => /* ts */ `import assert from 'assert' | ||
import axios from 'axios' | ||
import rest from '@feathersjs/rest-client' | ||
${ | ||
authStrategies.includes('local') | ||
? `import authenticationClient from '@feathersjs/authentication-client'` | ||
: '' | ||
} | ||
import { app } from '../${lib}/app' | ||
import { createClient } from '../${lib}/client' | ||
${authStrategies.includes('local') ? `import type { ${upperName}Data } from '../${lib}/client'` : ''} | ||
const port = app.get('port') | ||
const appUrl = \`http://\${app.get('host')}:\${port}\` | ||
describe('application client tests', () => { | ||
const client = createClient(rest(appUrl).axios(axios)) | ||
client.configure(authenticationClient()) | ||
before(async () => { | ||
await app.listen(port) | ||
}) | ||
after(async () => { | ||
await app.teardown() | ||
}) | ||
it('initialized the client', () => { | ||
assert.ok(client) | ||
}) | ||
${ | ||
authStrategies.includes('local') | ||
? ` | ||
it('creates and authenticates a user with email and password', async () => { | ||
const userData: ${upperName}Data = { | ||
email: 'someone@example.com', | ||
password: 'supersecret' | ||
} | ||
await client.service('users').create(userData) | ||
const { user, accessToken } = await client.authenticate({ | ||
strategy: 'local', | ||
...userData | ||
}) | ||
assert.ok(accessToken, 'Created access token for user') | ||
assert.ok(user, 'Includes user in authentication data') | ||
assert.strictEqual(user.password, undefined, 'Password is hidden to clients') | ||
await client.logout() | ||
// Remove the test user on the server | ||
await app.service('users').remove(user.${type === 'mongodb' ? '_id' : 'id'}) | ||
})` | ||
: '' | ||
} | ||
}) | ||
` | ||
|
||
export const generate = (ctx: AuthenticationGeneratorContext) => | ||
generator(ctx).then( | ||
renderSource( | ||
template, | ||
toFile<AuthenticationGeneratorContext>(({ test }) => test, 'client.test'), | ||
{ force: true } | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { generator, toFile, after, when } from '@feathershq/pinion' | ||
import { injectSource } from '../../commons' | ||
import { ServiceGeneratorContext } from '../../service' | ||
|
||
const importTemplate = /* ts */ `import type { AuthenticationService } from '@feathersjs/authentication' | ||
` | ||
const declarationTemplate = ` authentication: Pick<AuthenticationService, 'create' | 'remove'>` | ||
|
||
const toClientFile = toFile<ServiceGeneratorContext>(({ lib }) => [lib, 'client']) | ||
|
||
export const generate = async (ctx: ServiceGeneratorContext) => | ||
generator(ctx) | ||
.then(injectSource(importTemplate, after("from '@feathersjs/feathers'"), toClientFile)) | ||
.then( | ||
when( | ||
({ language }) => language === 'ts', | ||
injectSource(declarationTemplate, after('export interface ServiceTypes'), toClientFile) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.