sofa-api 0.11.2 → 0.12.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.
@@ -1,3 +1,7 @@
1
1
  import { GraphQLObjectType, GraphQLInputObjectType, GraphQLType } from 'graphql';
2
- export declare function buildSchemaObjectFromType(type: GraphQLObjectType | GraphQLInputObjectType): any;
3
- export declare function resolveFieldType(type: GraphQLType): any;
2
+ export declare function buildSchemaObjectFromType(type: GraphQLObjectType | GraphQLInputObjectType, opts: {
3
+ customScalars: Record<string, any>;
4
+ }): any;
5
+ export declare function resolveFieldType(type: GraphQLType, opts: {
6
+ customScalars: Record<string, any>;
7
+ }): any;
package/package.json CHANGED
@@ -1,22 +1,21 @@
1
1
  {
2
2
  "name": "sofa-api",
3
- "version": "0.11.2",
3
+ "version": "0.12.0",
4
4
  "description": "Create REST APIs with GraphQL",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
7
7
  "graphql": "^0.13.2 || ^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "@graphql-tools/utils": "8.6.5",
11
- "@types/js-yaml": "4.0.5",
12
- "ansi-colors": "4.1.1",
13
- "cross-undici-fetch": "0.1.28",
14
- "js-yaml": "4.1.0",
10
+ "@graphql-tools/utils": "8.12.0",
11
+ "ansi-colors": "4.1.3",
12
+ "@whatwg-node/fetch": "^0.4.3",
13
+ "@whatwg-node/server": "^0.4.1",
14
+ "itty-router": "^2.6.1",
15
+ "openapi-types": "12.0.2",
15
16
  "param-case": "3.0.4",
16
17
  "title-case": "3.0.3",
17
- "trouter": "3.2.0",
18
- "tslib": "2.3.1",
19
- "uuid": "8.3.2"
18
+ "tslib": "2.4.0"
20
19
  },
21
20
  "repository": {
22
21
  "type": "git",
package/router.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { Request as IttyRequest, Router } from 'itty-router';
2
+ import type { Sofa } from './sofa';
3
+ export declare type ErrorHandler = (errors: ReadonlyArray<any>) => Response;
4
+ declare type SofaRequest = IttyRequest & Request;
5
+ export declare function createRouter(sofa: Sofa): Router<SofaRequest, {}>;
6
+ export {};
package/sofa.d.ts CHANGED
@@ -1,10 +1,12 @@
1
- import { GraphQLSchema } from 'graphql';
2
- import { Ignore, ExecuteFn, OnRoute, Method } from './types';
3
- import { ErrorHandler } from './express';
1
+ import { GraphQLSchema, subscribe, execute } from 'graphql';
2
+ import { Ignore, OnRoute, Method, ContextFn, ContextValue } from './types';
3
+ import { ErrorHandler } from './router';
4
4
  interface RouteConfig {
5
5
  method?: Method;
6
6
  path?: string;
7
7
  responseStatus?: number;
8
+ tags?: string[];
9
+ description?: string;
8
10
  }
9
11
  export interface Route {
10
12
  method: Method;
@@ -14,7 +16,8 @@ export interface Route {
14
16
  export interface SofaConfig {
15
17
  basePath: string;
16
18
  schema: GraphQLSchema;
17
- execute?: ExecuteFn;
19
+ execute?: typeof execute;
20
+ subscribe?: typeof subscribe;
18
21
  /**
19
22
  * Treats an Object with an ID as not a model.
20
23
  * @example ["User", "Message.author"]
@@ -27,6 +30,7 @@ export interface SofaConfig {
27
30
  * Overwrites the default HTTP route.
28
31
  */
29
32
  routes?: Record<string, RouteConfig>;
33
+ context?: ContextFn | ContextValue;
30
34
  }
31
35
  export interface Sofa {
32
36
  basePath: string;
@@ -35,9 +39,12 @@ export interface Sofa {
35
39
  ignore: Ignore;
36
40
  depthLimit: number;
37
41
  routes?: Record<string, RouteConfig>;
38
- execute: ExecuteFn;
42
+ execute: typeof execute;
43
+ subscribe: typeof subscribe;
39
44
  onRoute?: OnRoute;
40
45
  errorHandler?: ErrorHandler;
46
+ contextFactory: ContextFn;
41
47
  }
42
48
  export declare function createSofa(config: SofaConfig): Sofa;
49
+ export declare function isContextFn(context: any): context is ContextFn;
43
50
  export {};
package/types.d.ts CHANGED
@@ -1,11 +1,16 @@
1
- import { GraphQLArgs, ExecutionResult, DocumentNode } from 'graphql';
1
+ import { DocumentNode } from 'graphql';
2
2
  export declare type ContextValue = Record<string, any>;
3
3
  export declare type Ignore = string[];
4
- export declare type ExecuteFn = (args: GraphQLArgs) => Promise<ExecutionResult<any>>;
5
4
  export declare type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
6
5
  export interface RouteInfo {
7
6
  document: DocumentNode;
8
7
  path: string;
9
8
  method: Method;
9
+ tags?: string[];
10
+ description?: string;
10
11
  }
11
12
  export declare type OnRoute = (info: RouteInfo) => void;
13
+ export declare type ContextFn = (init: {
14
+ req: any;
15
+ res: any;
16
+ }) => Promise<ContextValue> | ContextValue;
package/express.d.ts DELETED
@@ -1,25 +0,0 @@
1
- import type { Sofa } from './sofa';
2
- import type { ContextValue } from './types';
3
- export declare type ErrorHandler = (errors: ReadonlyArray<any>) => RouterError;
4
- declare type RouterRequest = {
5
- method: string;
6
- url: string;
7
- body: any;
8
- contextValue: ContextValue;
9
- };
10
- declare type RouterResult = {
11
- type: 'result';
12
- status: number;
13
- statusMessage?: string;
14
- body: any;
15
- };
16
- declare type RouterError = {
17
- type: 'error';
18
- status: number;
19
- statusMessage?: string;
20
- error: any;
21
- };
22
- declare type RouterResponse = RouterResult | RouterError;
23
- declare type Router = (request: RouterRequest) => Promise<null | RouterResponse>;
24
- export declare function createRouter(sofa: Sofa): Router;
25
- export {};
@@ -1,325 +0,0 @@
1
- declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
2
- export declare namespace OpenAPI {
3
- export interface Contact {
4
- /**
5
- * The identifying name of the contact person/organization.
6
- */
7
- name?: string;
8
- /**
9
- * The URL pointing to the contact information. MUST be in the format of a URL.
10
- */
11
- url?: string;
12
- /**
13
- * The email address of the contact person/organization. MUST be in the format of an email address.
14
- */
15
- email?: string;
16
- }
17
- export interface License {
18
- /**
19
- * The license name used for the API.
20
- */
21
- name: string;
22
- /**
23
- * A URL to the license used for the API. MUST be in the format of a URL.
24
- */
25
- url?: string;
26
- }
27
- export interface Info {
28
- /**
29
- * The title of the application.
30
- */
31
- title: string;
32
- /**
33
- * A short description of the application. CommonMark syntax MAY be used for rich text representation.
34
- */
35
- description?: string;
36
- /**
37
- * A URL to the Terms of Service for the API. MUST be in the format of a URL.
38
- */
39
- termsOfService?: string;
40
- /**
41
- * The contact information for the exposed API.
42
- */
43
- contact?: Contact;
44
- /**
45
- * The license information for the exposed API.
46
- */
47
- license?: License;
48
- /**
49
- * The version of the OpenAPI document (which is distinct from the OpenAPI Specification version or the API implementation version).
50
- */
51
- version: string;
52
- }
53
- export interface Reference {
54
- /**
55
- * The reference string.
56
- */
57
- $ref: string;
58
- }
59
- export interface Parameter {
60
- /**
61
- * The name of the parameter. Parameter names are case sensitive.
62
- * - If in is "path", the name field MUST correspond to the associated path segment from the path field in the Paths Object. See Path Templating for further information.
63
- * - If in is "header" and the name field is "Accept", "Content-Type" or "Authorization", the parameter definition SHALL be ignored.
64
- * - For all other cases, the name corresponds to the parameter name used by the in property
65
- */
66
- name: string;
67
- /**
68
- * The location of the parameter. Possible values are "query", "header", "path" or "cookie".
69
- */
70
- in: 'query' | 'header' | 'path' | 'cookie';
71
- /**
72
- * A brief description of the parameter. This could contain examples of use. CommonMark syntax MAY be used for rich text representation.
73
- */
74
- description?: string;
75
- /**
76
- * Determines whether this parameter is mandatory. If the parameter location is "path", this property is REQUIRED and its value MUST be true. Otherwise, the property MAY be included and its default value is false.
77
- */
78
- required: boolean;
79
- /**
80
- * Specifies that a parameter is deprecated and SHOULD be transitioned out of usage. Default value is false.
81
- */
82
- deprecated?: boolean;
83
- /**
84
- * Sets the ability to pass empty-valued parameters. This is valid only for query parameters and allows sending a parameter with an empty value. Default value is false. If style is used, and if behavior is n/a (cannot be serialized), the value of allowEmptyValue SHALL be ignored. Use of this property is NOT RECOMMENDED, as it is likely to be removed in a later revision.
85
- */
86
- allowEmptyValue?: boolean;
87
- schema?: Reference | Schema;
88
- }
89
- export interface RequestBody {
90
- /**
91
- * A brief description of the request body. This could contain examples of use. CommonMark syntax MAY be used for rich text representation.
92
- */
93
- description?: string;
94
- /**
95
- * The content of the request body. The key is a media type or media type range and the value describes it. For requests that match multiple keys, only the most specific key is applicable. e.g. text/plain overrides text/*
96
- */
97
- content: Array<string>;
98
- /**
99
- * Determines if the request body is required in the request. Defaults to false.
100
- */
101
- required?: boolean;
102
- }
103
- export type Header = Omit<Parameter, 'name' | 'in'>;
104
- export interface Link {
105
- /**
106
- * A relative or absolute reference to an OAS operation. This field is mutually exclusive of the operationId field, and MUST point to an Operation Object. Relative operationRef values MAY be used to locate an existing Operation Object in the OpenAPI definition.
107
- */
108
- operationRef?: string;
109
- /**
110
- * The name of an existing, resolvable OAS operation, as defined with a unique operationId. This field is mutually exclusive of the operationRef field.
111
- */
112
- operationId?: string;
113
- /**
114
- * A map representing parameters to pass to an operation as specified with operationId or identified via operationRef. The key is the parameter name to be used, whereas the value can be a constant or an expression to be evaluated and passed to the linked operation. The parameter name can be qualified using the parameter location [{in}.]{name} for operations that use the same parameter name in different locations (e.g. path.id).
115
- */
116
- parameters?: Record<string, any>;
117
- /**
118
- * A literal value or {expression} to use as a request body when calling the target operation.
119
- */
120
- requestBody?: any;
121
- /**
122
- * A description of the link. CommonMark syntax MAY be used for rich text representation.
123
- */
124
- description?: string;
125
- }
126
- interface Response {
127
- /**
128
- * A short description of the response. CommonMark syntax MAY be used for rich text representation.
129
- */
130
- description: string;
131
- headers?: {
132
- [header: string]: Header | Reference;
133
- };
134
- links?: {
135
- [link: string]: Link | Reference;
136
- };
137
- }
138
- interface Responses {
139
- default: Response | Reference;
140
- [httpStatusCode: number]: Response | Reference;
141
- }
142
- export interface Operation {
143
- /**
144
- * A list of tags for API documentation control.
145
- * Tags can be used for logical grouping of operations by resources or any other qualifier.
146
- */
147
- tags?: string[];
148
- /**
149
- * A short summary of what the operation does.
150
- */
151
- summary?: string;
152
- /**
153
- * A verbose explanation of the operation behavior. CommonMark syntax MAY be used for rich text representation.
154
- */
155
- description?: string;
156
- /**
157
- * Additional external documentation for this operation.
158
- */
159
- externalDocs?: {
160
- /**
161
- * The URL for the target documentation. Value MUST be in the format of a URL.
162
- */
163
- url: string;
164
- /**
165
- * A short description of the target documentation. CommonMark syntax MAY be used for rich text representation.
166
- */
167
- description?: string;
168
- };
169
- /**
170
- * Unique string used to identify the operation.
171
- * The id MUST be unique among all operations described in the API.
172
- * The operationId value is case-sensitive.
173
- * Tools and libraries MAY use the operationId to uniquely identify an operation,
174
- * therefore, it is RECOMMENDED to follow common programming naming conventions.
175
- */
176
- operationId?: string;
177
- /**
178
- * A list of parameters that are applicable for this operation.
179
- * If a parameter is already defined at the Path Item, the new definition will override it but can never remove it.
180
- * The list MUST NOT include duplicated parameters.
181
- * A unique parameter is defined by a combination of a name and location.
182
- * The list can use the Reference Object to link to parameters that are defined at the OpenAPI Object's components/parameters.
183
- */
184
- parameters?: Array<Parameter | Reference>;
185
- /**
186
- * The request body applicable for this operation. The requestBody is only supported in HTTP methods where the HTTP 1.1 specification RFC7231 has explicitly defined semantics for request bodies. In other cases where the HTTP spec is vague, requestBody SHALL be ignored by consumers.
187
- */
188
- requestBody?: RequestBody | Reference;
189
- /**
190
- * The list of possible responses as they are returned from executing this operation.
191
- */
192
- responses?: Responses;
193
- /**
194
- * Declares this operation to be deprecated. Consumers SHOULD refrain from usage of the declared operation. Default value is false.
195
- */
196
- deprecated?: boolean;
197
- }
198
- export interface PathItem {
199
- /**
200
- * Allows for an external definition of this path item.
201
- * The referenced structure MUST be in the format of a Path Item Object.
202
- * If there are conflicts between the referenced definition and this Path Item's definition, the behavior is undefined.
203
- */
204
- $ref?: string;
205
- /**
206
- * An optional, string summary, intended to apply to all operations in this path.
207
- */
208
- summary?: string;
209
- /**
210
- * An optional, string description, intended to apply to all operations in this path.
211
- * CommonMark syntax MAY be used for rich text representation.
212
- */
213
- description?: string;
214
- /**
215
- * A definition of a GET operation on this path.
216
- */
217
- get?: Operation;
218
- /**
219
- * A definition of a PUT operation on this path.
220
- */
221
- put?: Operation;
222
- /**
223
- * A definition of a POST operation on this path.
224
- */
225
- post?: Operation;
226
- /**
227
- * A definition of a DELETE operation on this path.
228
- */
229
- delete?: Operation;
230
- /**
231
- * A definition of a OPTIONS operation on this path.
232
- */
233
- options?: Operation;
234
- /**
235
- * A definition of a HEAD operation on this path.
236
- */
237
- head?: Operation;
238
- /**
239
- * A definition of a PATCH operation on this path.
240
- */
241
- patch?: Operation;
242
- /**
243
- * A definition of a TRACE operation on this path.
244
- */
245
- trace?: Operation;
246
- /**
247
- * A list of parameters that are applicable for all the operations described under this path. These parameters can be overridden at the operation level, but cannot be removed there. The list MUST NOT include duplicated parameters. A unique parameter is defined by a combination of a name and location. The list can use the Reference Object to link to parameters that are defined at the OpenAPI Object's components/parameters.
248
- */
249
- parameters?: Array<Parameter | Reference>;
250
- }
251
- export interface XML {
252
- /**
253
- * Replaces the name of the element/attribute used for the described schema property. When defined within items, it will affect the name of the individual XML elements within the list. When defined alongside type being array (outside the items), it will affect the wrapping element and only if wrapped is true. If wrapped is false, it will be ignored.
254
- */
255
- name?: string;
256
- /**
257
- * The URI of the namespace definition. Value MUST be in the form of an absolute URI.
258
- */
259
- namespace?: string;
260
- /**
261
- * The prefix to be used for the name.
262
- */
263
- prefix?: string;
264
- /**
265
- * Declares whether the property definition translates to an attribute instead of an element. Default value is false.
266
- */
267
- attribute?: boolean;
268
- /**
269
- * MAY be used only for an array definition. Signifies whether the array is wrapped (for example, <books><book/><book/></books>) or unwrapped (<book/><book/>). Default value is false. The definition takes effect only when defined alongside type being array (outside the items).
270
- */
271
- wrapped?: boolean;
272
- }
273
- export interface Schema {
274
- /**
275
- * Allows sending a null value for the defined schema. Default value is false.
276
- */
277
- nullable?: boolean;
278
- /**
279
- * Relevant only for Schema "properties" definitions. Declares the property as "read only". This means that it MAY be sent as part of a response but SHOULD NOT be sent as part of the request. If the property is marked as readOnly being true and is in the required list, the required will take effect on the response only. A property MUST NOT be marked as both readOnly and writeOnly being true. Default value is false.
280
- */
281
- readOnly?: boolean;
282
- /**
283
- * Relevant only for Schema "properties" definitions. Declares the property as "write only". Therefore, it MAY be sent as part of a request but SHOULD NOT be sent as part of the response. If the property is marked as writeOnly being true and is in the required list, the required will take effect on the request only. A property MUST NOT be marked as both readOnly and writeOnly being true. Default value is false.
284
- */
285
- writeOnly?: boolean;
286
- /**
287
- * This MAY be used only on properties schemas. It has no effect on root schemas. Adds additional metadata to describe the XML representation of this property.
288
- */
289
- xml?: XML;
290
- /**
291
- * Specifies that a schema is deprecated and SHOULD be transitioned out of usage. Default value is false.
292
- */
293
- deprecated?: boolean;
294
- }
295
- export interface Components {
296
- /**
297
- * An object to hold reusable Schema Objects.
298
- */
299
- schemas?: Record<string, Schema | Reference>;
300
- }
301
- export interface OpenAPI {
302
- /**
303
- * This string MUST be the semantic version number of the OpenAPI Specification version that the OpenAPI document uses.
304
- * The openapi field SHOULD be used by tooling specifications and clients to interpret the OpenAPI document.
305
- * This is not related to the API info.version string.
306
- */
307
- openapi: string;
308
- /**
309
- * Provides metadata about the API. The metadata MAY be used by tooling as required.
310
- */
311
- info: Info;
312
- /**
313
- * The available paths and operations for the API.
314
- */
315
- paths: {
316
- [path: string]: PathItem;
317
- };
318
- /**
319
- * An element to hold various schemas for the specification.
320
- */
321
- components?: Components;
322
- }
323
- export {};
324
- }
325
- export {};