Skip to content

Commit 94f4e6a

Browse files
committed
feat(endpoints): useEndpointsFactory -> createEndpointsFactory
1 parent 27a4118 commit 94f4e6a

5 files changed

Lines changed: 100 additions & 42 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

packages/endpoints/src/facade.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

packages/endpoints/src/generators.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,11 @@ export type GenerateEndpoints<Schemas extends EndpointSchemas> = {
149149
* method: 'POST',
150150
* params: { name: $type<string>(), 'gender?': $type<string>() },
151151
* response: $type<User>(),
152-
* }
152+
* },
153153
* });
154154
* userEndpoints.list().subscribe(users => console.log(users));
155155
* userEndpoints.create({ name: 'Char2s' }).subscribe(user => console.log(user));
156+
* userEndpoints.create({ name: 'Char2s', gender: "Male" }).subscribe(user => console.log(user));
156157
* ```
157158
*/
158159
export function generateEndpoints<Schemas extends EndpointSchemas>(

packages/endpoints/src/injection-token.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1+
export * from './convenience';
12
export * from './core';
2-
export * from './facade';
33
export * from './generators';
4-
export * from './injection-token';
54
export * from './invoker';

0 commit comments

Comments
 (0)