Skip to content

Commit bd59f91

Browse files
authored
feat(cli): Add authentication client to generated client (#2801)
1 parent f394098 commit bd59f91

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

packages/authentication-client/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ declare module '@feathersjs/feathers/lib/declarations' {
77
// eslint-disable-next-line @typescript-eslint/no-unused-vars
88
interface Application<Services, Settings> {
99
// eslint-disable-line
10-
io?: any
10+
io: any
1111
rest?: any
1212
authentication: AuthenticationClient
1313
authenticate: AuthenticationClient['authenticate']

packages/cli/src/app/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ export const generate = (ctx: AppGeneratorArguments) =>
176176
'@feathersjs/configuration',
177177
'@feathersjs/transport-commons',
178178
'@feathersjs/authentication',
179+
'@feathersjs/authentication-client',
179180
'winston'
180181
)
181182

@@ -211,8 +212,7 @@ export const generate = (ctx: AppGeneratorArguments) =>
211212
'cross-env',
212213
'prettier',
213214
'@feathersjs/cli',
214-
'@feathersjs/rest-client',
215-
'@feathersjs/authentication-client'
215+
'@feathersjs/rest-client'
216216
)
217217

218218
if (language === 'ts') {

packages/cli/src/app/templates/client.tpl.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,34 @@ import { generator, toFile } from '@feathershq/pinion'
22
import { renderSource } from '../../commons'
33
import { AppGeneratorContext } from '../index'
44

5-
const template = ({}: AppGeneratorContext) => /* ts */ `import { feathers } from '@feathersjs/feathers'
5+
const template = ({
6+
name,
7+
language
8+
}: AppGeneratorContext) => /* ts */ `import { feathers } from '@feathersjs/feathers'
69
import type { TransportConnection, Params } from '@feathersjs/feathers'
10+
import authenticationClient from '@feathersjs/authentication-client'
11+
import type { AuthenticationClientOptions } from '@feathersjs/authentication-client'
712
813
export interface ServiceTypes {
914
//
1015
}
1116
12-
export const createClient = <Configuration = any> (connection: TransportConnection<ServiceTypes>) => {
17+
/**
18+
* Returns a ${language === 'ts' ? 'typed' : ''} client for the ${name} app.
19+
*
20+
* @param connection The REST or Socket.io Feathers client connection
21+
* @param authenticationOptions Additional settings for the authentication client
22+
* @see https://dove.feathersjs.com/api/client.html
23+
* @returns The Feathers client application
24+
*/
25+
export const createClient = <Configuration = any> (
26+
connection: TransportConnection<ServiceTypes>,
27+
authenticationOptions: Partial<AuthenticationClientOptions> = {}
28+
) => {
1329
const client = feathers<ServiceTypes, Configuration>()
1430
1531
client.configure(connection)
32+
client.configure(authenticationClient(authenticationOptions))
1633
1734
return client
1835
}

packages/cli/src/authentication/templates/client.test.tpl.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ const appUrl = \`http://\${app.get('host')}:\${port}\`
2525
2626
describe('application client tests', () => {
2727
const client = createClient(rest(appUrl).axios(axios))
28-
29-
client.configure(authenticationClient())
3028
3129
before(async () => {
3230
await app.listen(port)

packages/cli/src/service/templates/client.tpl.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ export type {
2020
}
2121
`
2222

23-
const methodsTemplate = ({ camelName, upperName, className, type }: ServiceGeneratorContext) =>
24-
`const ${camelName}ServiceMethods = ['find', 'get', 'create', 'update', 'patch', 'remove'] as const
23+
const methodsTemplate = ({ camelName, upperName, className, type }: ServiceGeneratorContext) => `
24+
const ${camelName}ServiceMethods = ['find', 'get', 'create', 'update', 'patch', 'remove'] as const
2525
type ${upperName}ClientService = Pick<${className}${
26-
type !== 'custom' ? `<Params<${upperName}Query>>` : ''
27-
}, typeof ${camelName}ServiceMethods[number]>
28-
`
26+
type !== 'custom' ? `<Params<${upperName}Query>>` : ''
27+
}, typeof ${camelName}ServiceMethods[number]>`
2928

3029
const declarationTemplate = ({ path, upperName }: ServiceGeneratorContext) =>
3130
` '${path}': ${upperName}ClientService`
@@ -41,24 +40,18 @@ const toClientFile = toFile<ServiceGeneratorContext>(({ lib }) => [lib, 'client'
4140

4241
export const generate = async (ctx: ServiceGeneratorContext) =>
4342
generator(ctx)
44-
.then(
45-
injectSource(
46-
registrationTemplate,
47-
before('return client'),
48-
toFile<ServiceGeneratorContext>(({ lib }) => [lib, 'client'])
49-
)
50-
)
43+
.then(injectSource(registrationTemplate, before('return client'), toClientFile))
5144
.then(
5245
when(
5346
(ctx) => ctx.language === 'js',
54-
injectSource(methodsTemplate, before('\nexport const createClient'), toClientFile)
47+
injectSource(methodsTemplate, after('import authenticationClient'), toClientFile)
5548
)
5649
)
5750
.then(
5851
when(
5952
(ctx) => ctx.language === 'ts',
60-
injectSource(methodsTemplate, before('\nexport interface ServiceTypes'), toClientFile),
6153
injectSource(importTemplate, after("from '@feathersjs/feathers'"), toClientFile),
54+
injectSource(methodsTemplate, before('\nexport interface ServiceTypes'), toClientFile),
6255
injectSource(declarationTemplate, after('export interface ServiceTypes'), toClientFile)
6356
)
6457
)

packages/socketio/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ const debug = createDebug('@feathersjs/socketio')
1111
declare module '@feathersjs/feathers/lib/declarations' {
1212
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1313
interface Application<Services, Settings> {
14-
// eslint-disable-line
15-
io: Server
14+
io: any
1615
listen(options: any): Promise<http.Server>
1716
}
1817
}

0 commit comments

Comments
 (0)