|
| 1 | +import { inject, InjectionToken } from '@angular/core'; |
| 2 | + |
| 3 | +import { EndpointSchemas } from './core'; |
| 4 | +import { GenerateEndpoints, generateEndpoints } from './generators'; |
| 5 | +import { EndpointInvoker } from './invoker'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Convenience function that creates a typed `InjectionToken` with a default |
| 9 | + * value of a collection of endpoints generated from the given schemas, |
| 10 | + * through `generateEndpoints`. |
| 11 | + * |
| 12 | + * @see `generateEndpoints` - for manually generating endpoints |
| 13 | + * @see `createEndpointsFactory` - lower-level convenience function |
| 14 | + * |
| 15 | + * @example |
| 16 | + * ```ts |
| 17 | + * const USER_ENDPOINTS = new InjectionToken("USER_ENDPOINTS", { |
| 18 | + * factory: createEndpointsFactory({ |
| 19 | + * list: { |
| 20 | + * path: '/api/users', |
| 21 | + * method: 'GET', |
| 22 | + * params: null, |
| 23 | + * response: $type<User[]>(), |
| 24 | + * }, |
| 25 | + * create: { |
| 26 | + * path: '/api/users', |
| 27 | + * method: 'POST', |
| 28 | + * params: { name: $type<string>(), 'gender?': $type<string>() }, |
| 29 | + * response: $type<User>(), |
| 30 | + * }, |
| 31 | + * }), |
| 32 | + * }); |
| 33 | + * ``` |
| 34 | + * ```ts |
| 35 | + * private userEndpoints = inject(USER_ENDPOINTS); |
| 36 | + * ``` |
| 37 | + * ```ts |
| 38 | + * this.userEndpoints.list().subscribe(users => console.log(users)); |
| 39 | + * this.userEndpoints.create({ name: 'Char2s' }).subscribe(user => console.log(user)); |
| 40 | + * this.userEndpoints.create({ name: 'Char2s', gender: "Male" }).subscribe(user => console.log(user)); |
| 41 | + * ``` |
| 42 | + * |
| 43 | + */ |
| 44 | +export function createEndpointsInjectionToken<Schemas extends EndpointSchemas>( |
| 45 | + name: string, |
| 46 | + schemas: Schemas, |
| 47 | +): InjectionToken<GenerateEndpoints<Schemas>> { |
| 48 | + return new InjectionToken(name, { factory: createEndpointsFactory(schemas) }); |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Convenience function that creates a factory function, which, once invoked |
| 53 | + * within an `InjectionContext` or with dependencies supplied, returns a collection |
| 54 | + * of endpoint functions. |
| 55 | + * @param schemas the declaration schema for the intended endpoint functions |
| 56 | + * |
| 57 | + * @remarks Can be used as the factory function within `Provider` or |
| 58 | + * `InjectionToken` declarations. |
| 59 | + * |
| 60 | + * @see `generateEndpoints` - for manually generating endpoints |
| 61 | + * @see `createEndpointsInjectionToken` - higher-level convenience function for |
| 62 | + * using this function within an `InjectionToken` declaration |
| 63 | + * |
| 64 | + * @example |
| 65 | + * ```ts |
| 66 | + * const USER_ENDPOINTS = new InjectionToken("USER_ENDPOINTS", { |
| 67 | + * factory: createEndpointsFactory({ |
| 68 | + * list: { |
| 69 | + * path: '/api/users', |
| 70 | + * method: 'GET', |
| 71 | + * params: null, |
| 72 | + * response: $type<User[]>(), |
| 73 | + * }, |
| 74 | + * create: { |
| 75 | + * path: '/api/users', |
| 76 | + * method: 'POST', |
| 77 | + * params: { name: $type<string>(), 'gender?': $type<string>() }, |
| 78 | + * response: $type<User>(), |
| 79 | + * }, |
| 80 | + * }), |
| 81 | + * }); |
| 82 | + * ``` |
| 83 | + * ```ts |
| 84 | + * private userEndpoints = inject(USER_ENDPOINTS); |
| 85 | + * ``` |
| 86 | + * ```ts |
| 87 | + * this.userEndpoints.list().subscribe(users => console.log(users)); |
| 88 | + * this.userEndpoints.create({ name: 'Char2s' }).subscribe(user => console.log(user)); |
| 89 | + * this.userEndpoints.create({ name: 'Char2s', gender: "Male" }).subscribe(user => console.log(user)); |
| 90 | + * ``` |
| 91 | + */ |
| 92 | +export function createEndpointsFactory<Schemas extends EndpointSchemas>( |
| 93 | + schemas: Schemas, |
| 94 | +) { |
| 95 | + return (invoker = inject(EndpointInvoker)): GenerateEndpoints<Schemas> => |
| 96 | + generateEndpoints(invoker, schemas); |
| 97 | +} |
0 commit comments