sofa-api 0.15.1 → 0.15.2
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.
- package/ast.d.ts +1 -1
- package/index.d.ts +1 -1
- package/index.js +42 -34
- package/index.mjs +28 -20
- package/package.json +5 -6
- package/router.d.ts +4 -5
- package/subscriptions.d.ts +2 -2
- package/types.d.ts +7 -7
package/ast.d.ts
CHANGED
package/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { SofaConfig } from './sofa';
|
|
2
2
|
export { OpenAPI } from './open-api';
|
|
3
|
-
export declare function useSofa(config: SofaConfig): import("@whatwg-node/
|
|
3
|
+
export declare function useSofa(config: SofaConfig): import("@whatwg-node/router").Router<import("./types").DefaultSofaServerContext>;
|
package/index.js
CHANGED
|
@@ -6,12 +6,11 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
|
|
|
6
6
|
|
|
7
7
|
const tslib = require('tslib');
|
|
8
8
|
const graphql = require('graphql');
|
|
9
|
-
const ittyRouter = require('itty-router');
|
|
10
9
|
const utils = require('@graphql-tools/utils');
|
|
11
10
|
const paramCase = require('param-case');
|
|
12
11
|
const fetch = require('@whatwg-node/fetch');
|
|
13
12
|
const colors = _interopDefault(require('ansi-colors'));
|
|
14
|
-
const
|
|
13
|
+
const router = require('@whatwg-node/router');
|
|
15
14
|
const titleCase = require('title-case');
|
|
16
15
|
|
|
17
16
|
function getOperationInfo(doc) {
|
|
@@ -192,20 +191,27 @@ class SubscriptionManager {
|
|
|
192
191
|
});
|
|
193
192
|
// success
|
|
194
193
|
(() => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
195
|
-
var e_1,
|
|
194
|
+
var _a, e_1, _b, _c;
|
|
196
195
|
try {
|
|
197
|
-
for (var execution_1 = tslib.__asyncValues(execution), execution_1_1; execution_1_1 = yield execution_1.next(),
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
result
|
|
202
|
-
|
|
196
|
+
for (var _d = true, execution_1 = tslib.__asyncValues(execution), execution_1_1; execution_1_1 = yield execution_1.next(), _a = execution_1_1.done, !_a;) {
|
|
197
|
+
_c = execution_1_1.value;
|
|
198
|
+
_d = false;
|
|
199
|
+
try {
|
|
200
|
+
const result = _c;
|
|
201
|
+
yield this.sendData({
|
|
202
|
+
id,
|
|
203
|
+
result,
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
finally {
|
|
207
|
+
_d = true;
|
|
208
|
+
}
|
|
203
209
|
}
|
|
204
210
|
}
|
|
205
211
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
206
212
|
finally {
|
|
207
213
|
try {
|
|
208
|
-
if (
|
|
214
|
+
if (!_d && !_a && (_b = execution_1.return)) yield _b.call(execution_1);
|
|
209
215
|
}
|
|
210
216
|
finally { if (e_1) throw e_1.error; }
|
|
211
217
|
}
|
|
@@ -271,7 +277,7 @@ class SubscriptionManager {
|
|
|
271
277
|
|
|
272
278
|
function createRouter(sofa) {
|
|
273
279
|
logger.debug('[Sofa] Creating router');
|
|
274
|
-
const router =
|
|
280
|
+
const router$1 = router.createRouter({
|
|
275
281
|
base: sofa.basePath,
|
|
276
282
|
});
|
|
277
283
|
const queryType = sofa.schema.getQueryType();
|
|
@@ -279,7 +285,7 @@ function createRouter(sofa) {
|
|
|
279
285
|
const subscriptionManager = new SubscriptionManager(sofa);
|
|
280
286
|
if (queryType) {
|
|
281
287
|
Object.keys(queryType.getFields()).forEach((fieldName) => {
|
|
282
|
-
const route = createQueryRoute({ sofa, router, fieldName });
|
|
288
|
+
const route = createQueryRoute({ sofa, router: router$1, fieldName });
|
|
283
289
|
if (sofa.onRoute) {
|
|
284
290
|
sofa.onRoute(route);
|
|
285
291
|
}
|
|
@@ -287,13 +293,13 @@ function createRouter(sofa) {
|
|
|
287
293
|
}
|
|
288
294
|
if (mutationType) {
|
|
289
295
|
Object.keys(mutationType.getFields()).forEach((fieldName) => {
|
|
290
|
-
const route = createMutationRoute({ sofa, router, fieldName });
|
|
296
|
+
const route = createMutationRoute({ sofa, router: router$1, fieldName });
|
|
291
297
|
if (sofa.onRoute) {
|
|
292
298
|
sofa.onRoute(route);
|
|
293
299
|
}
|
|
294
300
|
});
|
|
295
301
|
}
|
|
296
|
-
router.post('/webhook', (request, serverContext) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
302
|
+
router$1.post('/webhook', (request, serverContext) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
297
303
|
const { subscription, variables, url } = yield request.json();
|
|
298
304
|
try {
|
|
299
305
|
const result = yield subscriptionManager.start({
|
|
@@ -301,7 +307,7 @@ function createRouter(sofa) {
|
|
|
301
307
|
variables,
|
|
302
308
|
url,
|
|
303
309
|
}, Object.assign(Object.assign({}, serverContext), { request }));
|
|
304
|
-
return new
|
|
310
|
+
return new router.Response(JSON.stringify(result), {
|
|
305
311
|
status: 200,
|
|
306
312
|
statusText: 'OK',
|
|
307
313
|
headers: {
|
|
@@ -310,13 +316,13 @@ function createRouter(sofa) {
|
|
|
310
316
|
});
|
|
311
317
|
}
|
|
312
318
|
catch (error) {
|
|
313
|
-
return new
|
|
319
|
+
return new router.Response(JSON.stringify(error), {
|
|
314
320
|
status: 500,
|
|
315
321
|
statusText: 'Subscription failed',
|
|
316
322
|
});
|
|
317
323
|
}
|
|
318
324
|
}));
|
|
319
|
-
router.post('/webhook/:id', (request, serverContext) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
325
|
+
router$1.post('/webhook/:id', (request, serverContext) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
320
326
|
var _a;
|
|
321
327
|
const id = (_a = request.params) === null || _a === void 0 ? void 0 : _a.id;
|
|
322
328
|
const body = yield request.json();
|
|
@@ -328,7 +334,7 @@ function createRouter(sofa) {
|
|
|
328
334
|
id,
|
|
329
335
|
variables,
|
|
330
336
|
}, contextValue);
|
|
331
|
-
return new
|
|
337
|
+
return new router.Response(JSON.stringify(result), {
|
|
332
338
|
status: 200,
|
|
333
339
|
statusText: 'OK',
|
|
334
340
|
headers: {
|
|
@@ -337,18 +343,18 @@ function createRouter(sofa) {
|
|
|
337
343
|
});
|
|
338
344
|
}
|
|
339
345
|
catch (error) {
|
|
340
|
-
return new
|
|
346
|
+
return new router.Response(JSON.stringify(error), {
|
|
341
347
|
status: 500,
|
|
342
348
|
statusText: 'Subscription failed to update',
|
|
343
349
|
});
|
|
344
350
|
}
|
|
345
351
|
}));
|
|
346
|
-
router.delete('/webhook/:id', (request) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
352
|
+
router$1.delete('/webhook/:id', (request) => tslib.__awaiter(this, void 0, void 0, function* () {
|
|
347
353
|
var _b;
|
|
348
354
|
const id = (_b = request.params) === null || _b === void 0 ? void 0 : _b.id;
|
|
349
355
|
try {
|
|
350
356
|
const result = yield subscriptionManager.stop(id);
|
|
351
|
-
return new
|
|
357
|
+
return new router.Response(JSON.stringify(result), {
|
|
352
358
|
status: 200,
|
|
353
359
|
statusText: 'OK',
|
|
354
360
|
headers: {
|
|
@@ -357,13 +363,13 @@ function createRouter(sofa) {
|
|
|
357
363
|
});
|
|
358
364
|
}
|
|
359
365
|
catch (error) {
|
|
360
|
-
return new
|
|
366
|
+
return new router.Response(JSON.stringify(error), {
|
|
361
367
|
status: 500,
|
|
362
368
|
statusText: 'Subscription failed to stop',
|
|
363
369
|
});
|
|
364
370
|
}
|
|
365
371
|
}));
|
|
366
|
-
return router;
|
|
372
|
+
return router$1;
|
|
367
373
|
}
|
|
368
374
|
function createQueryRoute({ sofa, router, fieldName, }) {
|
|
369
375
|
var _a, _b, _c, _d, _e, _f, _g;
|
|
@@ -394,7 +400,8 @@ function createQueryRoute({ sofa, router, fieldName, }) {
|
|
|
394
400
|
path: (_c = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.path) !== null && _c !== void 0 ? _c : getPath(fieldName, isSingle && hasIdArgument),
|
|
395
401
|
responseStatus: (_d = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.responseStatus) !== null && _d !== void 0 ? _d : 200,
|
|
396
402
|
};
|
|
397
|
-
|
|
403
|
+
const routerMethod = route.method.toLowerCase();
|
|
404
|
+
router[routerMethod](route.path, useHandler({ info, route, fieldName, sofa, operation }));
|
|
398
405
|
logger.debug(`[Router] ${fieldName} query available at ${route.method} ${route.path}`);
|
|
399
406
|
return {
|
|
400
407
|
document: operation,
|
|
@@ -430,7 +437,8 @@ function createMutationRoute({ sofa, router, fieldName, }) {
|
|
|
430
437
|
responseStatus: (_d = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.responseStatus) !== null && _d !== void 0 ? _d : 200,
|
|
431
438
|
};
|
|
432
439
|
const { method, path } = route;
|
|
433
|
-
|
|
440
|
+
const routerKey = method.toLowerCase();
|
|
441
|
+
router[routerKey](path, useHandler({ info, route, fieldName, sofa, operation }));
|
|
434
442
|
logger.debug(`[Router] ${fieldName} mutation available at ${method} ${path}`);
|
|
435
443
|
return {
|
|
436
444
|
document: operation,
|
|
@@ -500,12 +508,12 @@ function useHandler(config) {
|
|
|
500
508
|
status = 500;
|
|
501
509
|
}
|
|
502
510
|
if (errors.length === 1) {
|
|
503
|
-
return new
|
|
511
|
+
return new router.Response(JSON.stringify(errors[0]), {
|
|
504
512
|
status,
|
|
505
513
|
headers,
|
|
506
514
|
});
|
|
507
515
|
}
|
|
508
|
-
return new
|
|
516
|
+
return new router.Response(JSON.stringify({ errors }), {
|
|
509
517
|
status,
|
|
510
518
|
headers,
|
|
511
519
|
});
|
|
@@ -513,7 +521,7 @@ function useHandler(config) {
|
|
|
513
521
|
const errorHandler = sofa.errorHandler || defaultErrorHandler;
|
|
514
522
|
return errorHandler(result.errors);
|
|
515
523
|
}
|
|
516
|
-
return new
|
|
524
|
+
return new router.Response(JSON.stringify((_a = result.data) === null || _a === void 0 ? void 0 : _a[fieldName]), {
|
|
517
525
|
status: config.route.responseStatus,
|
|
518
526
|
headers: {
|
|
519
527
|
'Content-Type': 'application/json',
|
|
@@ -685,12 +693,12 @@ function resolveFieldType(type, opts) {
|
|
|
685
693
|
};
|
|
686
694
|
}
|
|
687
695
|
if (graphql.isScalarType(type)) {
|
|
688
|
-
|
|
696
|
+
const resolved = mapToPrimitive(type.name) ||
|
|
689
697
|
opts.customScalars[type.name] ||
|
|
690
|
-
((_a = type.extensions) === null || _a === void 0 ? void 0 : _a.jsonSchema)
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
698
|
+
((_a = type.extensions) === null || _a === void 0 ? void 0 : _a.jsonSchema) || {
|
|
699
|
+
type: 'object',
|
|
700
|
+
};
|
|
701
|
+
return Object.assign({}, resolved);
|
|
694
702
|
}
|
|
695
703
|
if (graphql.isEnumType(type)) {
|
|
696
704
|
return {
|
|
@@ -879,7 +887,7 @@ function OpenAPI({ schema, info, servers, components, security, tags, customScal
|
|
|
879
887
|
}
|
|
880
888
|
|
|
881
889
|
function useSofa(config) {
|
|
882
|
-
return
|
|
890
|
+
return createRouter(createSofa(config));
|
|
883
891
|
}
|
|
884
892
|
|
|
885
893
|
exports.OpenAPI = OpenAPI;
|
package/index.mjs
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { __awaiter, __asyncValues } from 'tslib';
|
|
2
2
|
import { getOperationAST, Kind, isScalarType, isEqualType, GraphQLBoolean, isInputObjectType, isObjectType, isNonNullType, execute, subscribe, getNamedType, isListType, isEnumType, parse, printType, isIntrospectionType } from 'graphql';
|
|
3
|
-
import { Router } from 'itty-router';
|
|
4
3
|
import { buildOperationNodeForField } from '@graphql-tools/utils';
|
|
5
4
|
import { paramCase } from 'param-case';
|
|
6
|
-
import { crypto, fetch
|
|
5
|
+
import { crypto, fetch } from '@whatwg-node/fetch';
|
|
7
6
|
import colors from 'ansi-colors';
|
|
8
|
-
import {
|
|
7
|
+
import { createRouter as createRouter$1, Response } from '@whatwg-node/router';
|
|
9
8
|
import { titleCase } from 'title-case';
|
|
10
9
|
|
|
11
10
|
function getOperationInfo(doc) {
|
|
@@ -186,20 +185,27 @@ class SubscriptionManager {
|
|
|
186
185
|
});
|
|
187
186
|
// success
|
|
188
187
|
(() => __awaiter(this, void 0, void 0, function* () {
|
|
189
|
-
var e_1,
|
|
188
|
+
var _a, e_1, _b, _c;
|
|
190
189
|
try {
|
|
191
|
-
for (var execution_1 = __asyncValues(execution), execution_1_1; execution_1_1 = yield execution_1.next(),
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
result
|
|
196
|
-
|
|
190
|
+
for (var _d = true, execution_1 = __asyncValues(execution), execution_1_1; execution_1_1 = yield execution_1.next(), _a = execution_1_1.done, !_a;) {
|
|
191
|
+
_c = execution_1_1.value;
|
|
192
|
+
_d = false;
|
|
193
|
+
try {
|
|
194
|
+
const result = _c;
|
|
195
|
+
yield this.sendData({
|
|
196
|
+
id,
|
|
197
|
+
result,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
finally {
|
|
201
|
+
_d = true;
|
|
202
|
+
}
|
|
197
203
|
}
|
|
198
204
|
}
|
|
199
205
|
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
200
206
|
finally {
|
|
201
207
|
try {
|
|
202
|
-
if (
|
|
208
|
+
if (!_d && !_a && (_b = execution_1.return)) yield _b.call(execution_1);
|
|
203
209
|
}
|
|
204
210
|
finally { if (e_1) throw e_1.error; }
|
|
205
211
|
}
|
|
@@ -265,7 +271,7 @@ class SubscriptionManager {
|
|
|
265
271
|
|
|
266
272
|
function createRouter(sofa) {
|
|
267
273
|
logger.debug('[Sofa] Creating router');
|
|
268
|
-
const router =
|
|
274
|
+
const router = createRouter$1({
|
|
269
275
|
base: sofa.basePath,
|
|
270
276
|
});
|
|
271
277
|
const queryType = sofa.schema.getQueryType();
|
|
@@ -388,7 +394,8 @@ function createQueryRoute({ sofa, router, fieldName, }) {
|
|
|
388
394
|
path: (_c = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.path) !== null && _c !== void 0 ? _c : getPath(fieldName, isSingle && hasIdArgument),
|
|
389
395
|
responseStatus: (_d = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.responseStatus) !== null && _d !== void 0 ? _d : 200,
|
|
390
396
|
};
|
|
391
|
-
|
|
397
|
+
const routerMethod = route.method.toLowerCase();
|
|
398
|
+
router[routerMethod](route.path, useHandler({ info, route, fieldName, sofa, operation }));
|
|
392
399
|
logger.debug(`[Router] ${fieldName} query available at ${route.method} ${route.path}`);
|
|
393
400
|
return {
|
|
394
401
|
document: operation,
|
|
@@ -424,7 +431,8 @@ function createMutationRoute({ sofa, router, fieldName, }) {
|
|
|
424
431
|
responseStatus: (_d = routeConfig === null || routeConfig === void 0 ? void 0 : routeConfig.responseStatus) !== null && _d !== void 0 ? _d : 200,
|
|
425
432
|
};
|
|
426
433
|
const { method, path } = route;
|
|
427
|
-
|
|
434
|
+
const routerKey = method.toLowerCase();
|
|
435
|
+
router[routerKey](path, useHandler({ info, route, fieldName, sofa, operation }));
|
|
428
436
|
logger.debug(`[Router] ${fieldName} mutation available at ${method} ${path}`);
|
|
429
437
|
return {
|
|
430
438
|
document: operation,
|
|
@@ -679,12 +687,12 @@ function resolveFieldType(type, opts) {
|
|
|
679
687
|
};
|
|
680
688
|
}
|
|
681
689
|
if (isScalarType(type)) {
|
|
682
|
-
|
|
690
|
+
const resolved = mapToPrimitive(type.name) ||
|
|
683
691
|
opts.customScalars[type.name] ||
|
|
684
|
-
((_a = type.extensions) === null || _a === void 0 ? void 0 : _a.jsonSchema)
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
692
|
+
((_a = type.extensions) === null || _a === void 0 ? void 0 : _a.jsonSchema) || {
|
|
693
|
+
type: 'object',
|
|
694
|
+
};
|
|
695
|
+
return Object.assign({}, resolved);
|
|
688
696
|
}
|
|
689
697
|
if (isEnumType(type)) {
|
|
690
698
|
return {
|
|
@@ -873,7 +881,7 @@ function OpenAPI({ schema, info, servers, components, security, tags, customScal
|
|
|
873
881
|
}
|
|
874
882
|
|
|
875
883
|
function useSofa(config) {
|
|
876
|
-
return
|
|
884
|
+
return createRouter(createSofa(config));
|
|
877
885
|
}
|
|
878
886
|
|
|
879
887
|
export { OpenAPI, useSofa };
|
package/package.json
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "sofa-api",
|
|
3
|
-
"version": "0.15.
|
|
3
|
+
"version": "0.15.2",
|
|
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": "9.1.
|
|
11
|
-
"@whatwg-node/fetch": "^0.
|
|
12
|
-
"@whatwg-node/
|
|
10
|
+
"@graphql-tools/utils": "9.1.3",
|
|
11
|
+
"@whatwg-node/fetch": "^0.6.0",
|
|
12
|
+
"@whatwg-node/router": "^0.1.2",
|
|
13
13
|
"ansi-colors": "4.1.3",
|
|
14
|
-
"
|
|
15
|
-
"openapi-types": "12.0.2",
|
|
14
|
+
"openapi-types": "12.1.0",
|
|
16
15
|
"param-case": "3.0.4",
|
|
17
16
|
"title-case": "3.0.3",
|
|
18
17
|
"tslib": "2.4.1"
|
package/router.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { Request as IttyRequest, Router } from 'itty-router';
|
|
2
1
|
import type { Sofa } from './sofa';
|
|
3
|
-
|
|
2
|
+
import type { DefaultSofaServerContext } from './types';
|
|
3
|
+
import { Router } from '@whatwg-node/router';
|
|
4
|
+
export type ErrorHandler = (errors: ReadonlyArray<any>) => Response;
|
|
4
5
|
declare module 'graphql' {
|
|
5
6
|
interface GraphQLHTTPErrorExtensions {
|
|
6
7
|
status?: number;
|
|
@@ -10,6 +11,4 @@ declare module 'graphql' {
|
|
|
10
11
|
http?: GraphQLHTTPErrorExtensions;
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
|
-
declare
|
|
14
|
-
export declare function createRouter(sofa: Sofa): Router<SofaRequest, {}>;
|
|
15
|
-
export {};
|
|
14
|
+
export declare function createRouter(sofa: Sofa): Router<DefaultSofaServerContext>;
|
package/subscriptions.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { ExecutionResult } from 'graphql';
|
|
2
2
|
import type { ContextValue } from './types';
|
|
3
3
|
import type { Sofa } from './sofa';
|
|
4
|
-
export
|
|
5
|
-
export
|
|
4
|
+
export type ID = string;
|
|
5
|
+
export type SubscriptionFieldName = string;
|
|
6
6
|
export interface StartSubscriptionEvent {
|
|
7
7
|
subscription: SubscriptionFieldName;
|
|
8
8
|
variables: any;
|
package/types.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { DefaultServerAdapterContext } from '@whatwg-node/
|
|
1
|
+
import { DefaultServerAdapterContext } from '@whatwg-node/router';
|
|
2
2
|
import { DocumentNode } from 'graphql';
|
|
3
|
-
export
|
|
4
|
-
export
|
|
5
|
-
export
|
|
3
|
+
export type ContextValue = Record<string, any>;
|
|
4
|
+
export type Ignore = string[];
|
|
5
|
+
export type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
6
6
|
export interface RouteInfo {
|
|
7
7
|
document: DocumentNode;
|
|
8
8
|
path: string;
|
|
@@ -10,8 +10,8 @@ export interface RouteInfo {
|
|
|
10
10
|
tags?: string[];
|
|
11
11
|
description?: string;
|
|
12
12
|
}
|
|
13
|
-
export
|
|
14
|
-
export
|
|
15
|
-
export
|
|
13
|
+
export type OnRoute = (info: RouteInfo) => void;
|
|
14
|
+
export type ContextFn = (serverContext: DefaultSofaServerContext) => Promise<ContextValue> | ContextValue;
|
|
15
|
+
export type DefaultSofaServerContext = DefaultServerAdapterContext & {
|
|
16
16
|
request: Request;
|
|
17
17
|
};
|