@rest-vir/implement-service 0.13.0 → 0.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,10 +11,10 @@ import { type ContextInit } from './service-context-init.js';
11
11
  * @category Package : @rest-vir/implement-service
12
12
  * @package [`@rest-vir/implement-service`](https://www.npmjs.com/package/@rest-vir/implement-service)
13
13
  */
14
- export type ImplementedEndpoint<Context = any, ServiceName extends string = any, SpecificEndpoint extends EndpointDefinition = GenericEndpointDefinition> = Overwrite<SpecificEndpoint, {
14
+ export type ImplementedEndpoint<Context = any, SpecificEndpoint extends EndpointDefinition = GenericEndpointDefinition, ServiceName extends string = any> = Overwrite<SpecificEndpoint, {
15
15
  service: GenericServiceImplementation;
16
16
  }> & {
17
- implementation: EndpointImplementation<Context, ServiceName>;
17
+ implementation: EndpointImplementation<Context, NoParam, ServiceName>;
18
18
  };
19
19
  /**
20
20
  * A super generic service implementation that be assigned to from any concrete service
@@ -51,9 +51,9 @@ export type EndpointImplementationOutput<ResponseDataType = unknown> = EndpointI
51
51
  * @category Package : @rest-vir/implement-service
52
52
  * @package [`@rest-vir/implement-service`](https://www.npmjs.com/package/@rest-vir/implement-service)
53
53
  */
54
- export type EndpointImplementationParams<Context = any, ServiceName extends string = any, SpecificEndpoint extends EndpointDefinition | NoParam = NoParam> = {
54
+ export type EndpointImplementationParams<Context = any, SpecificEndpoint extends EndpointDefinition | NoParam = NoParam, ServiceName extends string = any> = {
55
55
  pathParams: SpecificEndpoint extends NoParam ? Readonly<Record<string, string>> : PathParams<Exclude<SpecificEndpoint, NoParam>['path']> extends string ? Readonly<Record<PathParams<Exclude<SpecificEndpoint, NoParam>['path']>, string>> : Readonly<Record<string, string>>;
56
- context: Context;
56
+ context: NoInfer<Context>;
57
57
  method: IsEqual<Extract<SpecificEndpoint, NoParam>, NoParam> extends true ? HttpMethod : ExtractKeysWithMatchingValues<Exclude<SpecificEndpoint, NoParam>['methods'], true> extends infer AvailableMethod ? IsNever<AvailableMethod> extends true ? HttpMethod : AvailableMethod : never;
58
58
  endpoint: IsEqual<Extract<SpecificEndpoint, NoParam>, NoParam> extends true ? EndpointDefinition : SpecificEndpoint;
59
59
  service: MinimalService<ServiceName>;
@@ -106,7 +106,7 @@ export type GenericEndpointImplementationParams = {
106
106
  * @category Package : @rest-vir/implement-service
107
107
  * @package [`@rest-vir/implement-service`](https://www.npmjs.com/package/@rest-vir/implement-service)
108
108
  */
109
- export type EndpointImplementation<Context = any, ServiceName extends string = any, SpecificEndpoint extends EndpointDefinition | NoParam = NoParam> = IsEqual<Extract<SpecificEndpoint, NoParam>, NoParam> extends true ? (params: GenericEndpointImplementationParams) => any : (params: Readonly<EndpointImplementationParams<Context, ServiceName, SpecificEndpoint>>) => MaybePromise<EndpointImplementationOutput<WithFinalEndpointProps<SpecificEndpoint, any>['ResponseType']>>;
109
+ export type EndpointImplementation<Context = any, SpecificEndpoint extends EndpointDefinition | NoParam = NoParam, ServiceName extends string = any> = IsEqual<Extract<SpecificEndpoint, NoParam>, NoParam> extends true ? (params: GenericEndpointImplementationParams) => any : (params: Readonly<EndpointImplementationParams<NoInfer<Context>, SpecificEndpoint, ServiceName>>) => MaybePromise<EndpointImplementationOutput<WithFinalEndpointProps<SpecificEndpoint, any>['ResponseType']>>;
110
110
  /**
111
111
  * All endpoint implementations to match the service definition's endpoints.
112
112
  *
@@ -114,8 +114,8 @@ export type EndpointImplementation<Context = any, ServiceName extends string = a
114
114
  * @category Package : @rest-vir/implement-service
115
115
  * @package [`@rest-vir/implement-service`](https://www.npmjs.com/package/@rest-vir/implement-service)
116
116
  */
117
- export type EndpointImplementations<Context = any, ServiceName extends string = any, EndpointsInit extends BaseServiceEndpointsInit | NoParam = NoParam> = EndpointsInit extends NoParam ? Record<EndpointPathBase, EndpointImplementation> : {
118
- [EndpointPath in keyof EndpointsInit]: EndpointsInit[EndpointPath] extends EndpointInit ? EndpointPath extends EndpointPathBase ? EndpointImplementation<Context, ServiceName, WithFinalEndpointProps<EndpointsInit[EndpointPath], EndpointPath>> : never : never;
117
+ export type EndpointImplementations<Context = any, EndpointsInit extends BaseServiceEndpointsInit | NoParam = NoParam, ServiceName extends string = any> = EndpointsInit extends NoParam ? Record<EndpointPathBase, EndpointImplementation> : {
118
+ [EndpointPath in keyof EndpointsInit]: EndpointsInit[EndpointPath] extends EndpointInit ? EndpointPath extends EndpointPathBase ? EndpointImplementation<NoInfer<Context>, WithFinalEndpointProps<EndpointsInit[EndpointPath], EndpointPath>, ServiceName> : never : never;
119
119
  };
120
120
  /**
121
121
  * Asserts that all endpoint implementations are valid.
@@ -21,7 +21,7 @@ export type CustomErrorHandler = (error: Error) => MaybePromise<void>;
21
21
  * @category Package : @rest-vir/implement-service
22
22
  * @package [`@rest-vir/implement-service`](https://www.npmjs.com/package/@rest-vir/implement-service)
23
23
  */
24
- export type ServiceImplementationInit<Context, ServiceName extends string, EndpointsInit extends BaseServiceEndpointsInit, WebSocketsInit extends BaseServiceWebSocketsInit> = {
24
+ export type ServiceImplementationInit<ServiceName extends string, EndpointsInit extends BaseServiceEndpointsInit, WebSocketsInit extends BaseServiceWebSocketsInit> = {
25
25
  /** Set all custom headers that you'll be using here so that CORS will allow them. */
26
26
  customHeaders?: string[];
27
27
  service: ServiceDefinition<ServiceName, EndpointsInit, WebSocketsInit>;
@@ -31,11 +31,7 @@ export type ServiceImplementationInit<Context, ServiceName extends string, Endpo
31
31
  * keys will fallback to the efault logger.
32
32
  */
33
33
  logger?: ServiceLoggerOption;
34
- } & (IsEqual<Context, undefined> extends true ? {
35
- createContext?: undefined | ContextInit<Context, NoInfer<ServiceName>, NoInfer<EndpointsInit>, NoInfer<WebSocketsInit>>;
36
- } : {
37
- createContext: ContextInit<Context, NoInfer<ServiceName>, NoInfer<EndpointsInit>, NoInfer<WebSocketsInit>>;
38
- });
34
+ };
39
35
  /**
40
36
  * Parameters for implementations for {@link implementService}.
41
37
  *
@@ -46,7 +42,7 @@ export type ServiceImplementationInit<Context, ServiceName extends string, Endpo
46
42
  export type ServiceImplementationsParams<Context, ServiceName extends string, EndpointsInit extends BaseServiceEndpointsInit, WebSocketsInit extends BaseServiceWebSocketsInit> = (KeyCount<OmitIndexSignature<EndpointsInit>> extends 0 ? {
47
43
  endpoints?: never;
48
44
  } : {
49
- endpoints: EndpointImplementations<NoInfer<Context>, NoInfer<ServiceName>, NoInfer<EndpointsInit>>;
45
+ endpoints: EndpointImplementations<NoInfer<Context>, NoInfer<EndpointsInit>, NoInfer<ServiceName>>;
50
46
  }) & (KeyCount<OmitIndexSignature<WebSocketsInit>> extends 0 ? {
51
47
  webSockets?: never;
52
48
  } : {
@@ -62,9 +58,9 @@ export type ServiceImplementationsParams<Context, ServiceName extends string, En
62
58
  * @category Package : @rest-vir/implement-service
63
59
  * @package [`@rest-vir/implement-service`](https://www.npmjs.com/package/@rest-vir/implement-service)
64
60
  */
65
- export declare function implementService<const ServiceName extends string, const EndpointsInit extends BaseServiceEndpointsInit, const WebSocketsInit extends BaseServiceWebSocketsInit, const Context = undefined>(
61
+ export declare function implementService<const ServiceName extends string, const EndpointsInit extends BaseServiceEndpointsInit, const WebSocketsInit extends BaseServiceWebSocketsInit, const Context = NoParam>(
66
62
  /** Init must be first so that TypeScript can infer the type for `Context`. */
67
- { service, createContext, logger, customHeaders, }: ServiceImplementationInit<Context, ServiceName, EndpointsInit, WebSocketsInit>, { endpoints: endpointImplementations, webSockets: webSocketImplementations, }: ServiceImplementationsParams<NoInfer<Context>, NoInfer<ServiceName>, NoInfer<EndpointsInit>, NoInfer<WebSocketsInit>>): ServiceImplementation<Context, ServiceName, EndpointsInit, WebSocketsInit>;
63
+ { service, logger, customHeaders, }: ServiceImplementationInit<ServiceName, EndpointsInit, WebSocketsInit>, createContext: IsEqual<NoInfer<Context>, undefined> extends true ? undefined | ContextInit<Context, NoInfer<ServiceName>, NoInfer<EndpointsInit>, NoInfer<WebSocketsInit>> : IsEqual<NoInfer<Context>, NoParam> extends true ? undefined | ContextInit<Context, NoInfer<ServiceName>, NoInfer<EndpointsInit>, NoInfer<WebSocketsInit>> : ContextInit<Context, NoInfer<ServiceName>, NoInfer<EndpointsInit>, NoInfer<WebSocketsInit>>, { endpoints: endpointImplementations, webSockets: webSocketImplementations, }: ServiceImplementationsParams<NoInfer<Context>, NoInfer<ServiceName>, NoInfer<EndpointsInit>, NoInfer<WebSocketsInit>>): ServiceImplementation<NoInfer<Context>, ServiceName, EndpointsInit, WebSocketsInit>;
68
64
  /**
69
65
  * A finalized service implementation created by {@link implementService}.
70
66
  *
@@ -74,7 +70,7 @@ export declare function implementService<const ServiceName extends string, const
74
70
  */
75
71
  export type ServiceImplementation<Context = any, ServiceName extends string = any, EndpointsInit extends BaseServiceEndpointsInit | NoParam = NoParam, WebSocketsInit extends BaseServiceWebSocketsInit | NoParam = NoParam> = Omit<ServiceDefinition<ServiceName, EndpointsInit, WebSocketsInit>, 'endpoints' | 'webSockets'> & {
76
72
  endpoints: {
77
- [EndpointPath in keyof ServiceDefinition<ServiceName, EndpointsInit, WebSocketsInit>['endpoints']]: EndpointPath extends EndpointPathBase ? ImplementedEndpoint<Context, ServiceName, ServiceDefinition<ServiceName, EndpointsInit, WebSocketsInit>['endpoints'][EndpointPath]> : never;
73
+ [EndpointPath in keyof ServiceDefinition<ServiceName, EndpointsInit, WebSocketsInit>['endpoints']]: EndpointPath extends EndpointPathBase ? ImplementedEndpoint<Context, ServiceDefinition<ServiceName, EndpointsInit, WebSocketsInit>['endpoints'][EndpointPath], ServiceName> : never;
78
74
  };
79
75
  webSockets: {
80
76
  [WebSocketPath in keyof ServiceDefinition<ServiceName, EndpointsInit, WebSocketsInit>['webSockets']]: WebSocketPath extends EndpointPathBase ? ImplementedWebSocket<Context, ServiceName, ServiceDefinition<ServiceName, EndpointsInit, WebSocketsInit>['webSockets'][WebSocketPath]> : never;
@@ -15,7 +15,7 @@ import { assertValidWebSocketImplementations, } from './implement-web-socket.js'
15
15
  */
16
16
  export function implementService(
17
17
  /** Init must be first so that TypeScript can infer the type for `Context`. */
18
- { service, createContext, logger, customHeaders, }, { endpoints: endpointImplementations, webSockets: webSocketImplementations, }) {
18
+ { service, logger, customHeaders, }, createContext, { endpoints: endpointImplementations, webSockets: webSocketImplementations, }) {
19
19
  assertValidEndpointImplementations(service, endpointImplementations || {});
20
20
  assertValidWebSocketImplementations(service, webSocketImplementations || {});
21
21
  const endpoints = mapObjectValues(service.endpoints, (endpointPath, endpoint) => {
@@ -5,13 +5,13 @@ import { type RequireExactlyOne } from 'type-fest';
5
5
  import { type ServerRequest, type ServerResponse } from '../util/data.js';
6
6
  import { EndpointImplementationErrorOutput, type RunningServerInfo } from './implement-endpoint.js';
7
7
  /**
8
- * User-defined service implementation Context generator.
8
+ * Output of {@link ContextInit}.
9
9
  *
10
10
  * @category Internal
11
11
  * @category Package : @rest-vir/implement-service
12
12
  * @package [`@rest-vir/implement-service`](https://www.npmjs.com/package/@rest-vir/implement-service)
13
13
  */
14
- export type ContextInit<Context, ServiceName extends string, EndpointsInit extends BaseServiceEndpointsInit | NoParam, WebSocketsInit extends BaseServiceWebSocketsInit | NoParam> = (params: Readonly<ContextInitParameters<ServiceName, EndpointsInit, WebSocketsInit>>) => MaybePromise<RequireExactlyOne<{
14
+ export type ContextInitOutput<Context> = RequireExactlyOne<{
15
15
  /** The context created for this request. */
16
16
  context: Context;
17
17
  /**
@@ -19,7 +19,15 @@ export type ContextInit<Context, ServiceName extends string, EndpointsInit exten
19
19
  * with the specified status code and other options.
20
20
  */
21
21
  reject: EndpointImplementationErrorOutput;
22
- }>>;
22
+ }>;
23
+ /**
24
+ * User-defined service implementation Context generator.
25
+ *
26
+ * @category Internal
27
+ * @category Package : @rest-vir/implement-service
28
+ * @package [`@rest-vir/implement-service`](https://www.npmjs.com/package/@rest-vir/implement-service)
29
+ */
30
+ export type ContextInit<Context, ServiceName extends string, EndpointsInit extends BaseServiceEndpointsInit | NoParam, WebSocketsInit extends BaseServiceWebSocketsInit | NoParam> = (params: Readonly<ContextInitParameters<ServiceName, EndpointsInit, WebSocketsInit>>) => MaybePromise<ContextInitOutput<Context>>;
23
31
  /**
24
32
  * Parameters for {@link ContextInit}.
25
33
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rest-vir/implement-service",
3
- "version": "0.13.0",
3
+ "version": "0.14.0",
4
4
  "description": "Implement a service defined by @rest-vir/define-service.",
5
5
  "keywords": [
6
6
  "rest",
@@ -39,16 +39,16 @@
39
39
  "test:update": "npm test update"
40
40
  },
41
41
  "dependencies": {
42
- "@augment-vir/assert": "^31.11.0",
43
- "@augment-vir/common": "^31.11.0",
44
- "@rest-vir/define-service": "^0.13.0",
42
+ "@augment-vir/assert": "^31.14.1",
43
+ "@augment-vir/common": "^31.14.1",
44
+ "@rest-vir/define-service": "^0.14.0",
45
45
  "@types/ws": "^8.18.1",
46
- "fastify": "^5.3.0",
46
+ "fastify": "^5.3.2",
47
47
  "path-to-regexp": "^8.2.0",
48
48
  "type-fest": "^4.40.0"
49
49
  },
50
50
  "devDependencies": {
51
- "@augment-vir/test": "^31.11.0",
51
+ "@augment-vir/test": "^31.14.1",
52
52
  "@types/node": "^22.14.1",
53
53
  "c8": "^10.1.3",
54
54
  "istanbul-smart-text-reporter": "^1.1.5",