sofa-api 0.18.2 → 0.18.5

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/cjs/ast.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getOperationInfo = void 0;
3
+ exports.getOperationInfo = getOperationInfo;
4
4
  const graphql_1 = require("graphql");
5
5
  function getOperationInfo(doc) {
6
6
  const op = (0, graphql_1.getOperationAST)(doc, null);
@@ -13,4 +13,3 @@ function getOperationInfo(doc) {
13
13
  variables: op.variableDefinitions || [],
14
14
  };
15
15
  }
16
- exports.getOperationInfo = getOperationInfo;
package/cjs/common.js CHANGED
@@ -1,12 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isNil = exports.convertName = void 0;
3
+ exports.convertName = convertName;
4
+ exports.isNil = isNil;
4
5
  const param_case_1 = require("param-case");
5
6
  function convertName(name) {
6
7
  return (0, param_case_1.paramCase)(name);
7
8
  }
8
- exports.convertName = convertName;
9
9
  function isNil(val) {
10
10
  return val == null;
11
11
  }
12
- exports.isNil = isNil;
package/cjs/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.useSofa = exports.OpenAPI = void 0;
3
+ exports.OpenAPI = void 0;
4
+ exports.useSofa = useSofa;
4
5
  const router_1 = require("./router");
5
6
  const sofa_1 = require("./sofa");
6
7
  var open_api_1 = require("./open-api");
@@ -8,4 +9,3 @@ Object.defineProperty(exports, "OpenAPI", { enumerable: true, get: function () {
8
9
  function useSofa(config) {
9
10
  return (0, router_1.createRouter)((0, sofa_1.createSofa)(config));
10
11
  }
11
- exports.useSofa = useSofa;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpenAPI = void 0;
3
+ exports.OpenAPI = OpenAPI;
4
4
  const graphql_1 = require("graphql");
5
5
  const types_1 = require("./types");
6
6
  const operations_1 = require("./operations");
@@ -59,4 +59,3 @@ function OpenAPI({ schema, info, servers, components, security, tags, customScal
59
59
  },
60
60
  };
61
61
  }
62
- exports.OpenAPI = OpenAPI;
@@ -1,6 +1,11 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveVariableDescription = exports.isInPath = exports.resolveResponse = exports.resolveParamSchema = exports.resolveRequestBody = exports.buildPathFromOperation = void 0;
3
+ exports.buildPathFromOperation = buildPathFromOperation;
4
+ exports.resolveRequestBody = resolveRequestBody;
5
+ exports.resolveParamSchema = resolveParamSchema;
6
+ exports.resolveResponse = resolveResponse;
7
+ exports.isInPath = isInPath;
8
+ exports.resolveVariableDescription = resolveVariableDescription;
4
9
  const graphql_1 = require("graphql");
5
10
  const ast_1 = require("../ast");
6
11
  const utils_1 = require("./utils");
@@ -48,7 +53,6 @@ function buildPathFromOperation({ url, schema, operation, useRequestBody, tags,
48
53
  },
49
54
  };
50
55
  }
51
- exports.buildPathFromOperation = buildPathFromOperation;
52
56
  function resolveEnumTypes(schema) {
53
57
  const enumTypes = Object.values(schema.getTypeMap())
54
58
  .filter(graphql_1.isEnumType);
@@ -95,7 +99,6 @@ function resolveRequestBody(variables, schema, operation, opts) {
95
99
  ...(required.length ? { required } : {}),
96
100
  };
97
101
  }
98
- exports.resolveRequestBody = resolveRequestBody;
99
102
  // array -> [type]
100
103
  // type -> $ref
101
104
  // scalar -> swagger primitive
@@ -114,7 +117,6 @@ function resolveParamSchema(type, opts) {
114
117
  opts.customScalars[type.name.value] ||
115
118
  opts.enumTypes[type.name.value] || { $ref: (0, utils_1.mapToRef)(type.name.value) });
116
119
  }
117
- exports.resolveParamSchema = resolveParamSchema;
118
120
  function resolveResponse({ schema, operation, opts, }) {
119
121
  const operationType = operation.operation;
120
122
  const rootField = operation.selectionSet.selections[0];
@@ -131,11 +133,9 @@ function resolveResponse({ schema, operation, opts, }) {
131
133
  }
132
134
  }
133
135
  }
134
- exports.resolveResponse = resolveResponse;
135
136
  function isInPath(url, param) {
136
137
  return url.includes(`:${param}`) || url.includes(`{${param}}`);
137
138
  }
138
- exports.isInPath = isInPath;
139
139
  function getOperationFieldNode(schema, operation) {
140
140
  const selection = operation.selectionSet.selections[0];
141
141
  const fieldName = selection.name.value;
@@ -158,7 +158,6 @@ function resolveVariableDescription(schema, operation, variable) {
158
158
  const argument = fieldNode?.arguments?.find((arg) => arg.name.value === variable.variable.name.value);
159
159
  return argument?.description?.value;
160
160
  }
161
- exports.resolveVariableDescription = resolveVariableDescription;
162
161
  function isObjectTypeDefinitionNode(node) {
163
162
  return node.kind === graphql_1.Kind.OBJECT_TYPE_DEFINITION;
164
163
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.resolveFieldType = exports.buildSchemaObjectFromType = void 0;
3
+ exports.buildSchemaObjectFromType = buildSchemaObjectFromType;
4
+ exports.resolveFieldType = resolveFieldType;
4
5
  const graphql_1 = require("graphql");
5
6
  const utils_1 = require("./utils");
6
7
  function buildSchemaObjectFromType(type, opts) {
@@ -24,7 +25,6 @@ function buildSchemaObjectFromType(type, opts) {
24
25
  ...(type.description ? { description: type.description } : {}),
25
26
  };
26
27
  }
27
- exports.buildSchemaObjectFromType = buildSchemaObjectFromType;
28
28
  function resolveField(field, opts) {
29
29
  return resolveFieldType(field.type, opts);
30
30
  }
@@ -69,4 +69,3 @@ function resolveFieldType(type, opts) {
69
69
  type: 'object',
70
70
  };
71
71
  }
72
- exports.resolveFieldType = resolveFieldType;
@@ -1,6 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.normalizePathParamForOpenAPI = exports.mapToRef = exports.mapToPrimitive = void 0;
3
+ exports.mapToPrimitive = mapToPrimitive;
4
+ exports.mapToRef = mapToRef;
5
+ exports.normalizePathParamForOpenAPI = normalizePathParamForOpenAPI;
4
6
  function mapToPrimitive(type) {
5
7
  const formatMap = {
6
8
  Int: {
@@ -25,11 +27,9 @@ function mapToPrimitive(type) {
25
27
  return formatMap[type];
26
28
  }
27
29
  }
28
- exports.mapToPrimitive = mapToPrimitive;
29
30
  function mapToRef(type) {
30
31
  return `#/components/schemas/${type}`;
31
32
  }
32
- exports.mapToRef = mapToRef;
33
33
  function normalizePathParamForOpenAPI(path) {
34
34
  const pathParts = path.split('/');
35
35
  const normalizedPathParts = pathParts.map((part) => {
@@ -40,4 +40,3 @@ function normalizePathParamForOpenAPI(path) {
40
40
  });
41
41
  return normalizedPathParts.join('/');
42
42
  }
43
- exports.normalizePathParamForOpenAPI = normalizePathParamForOpenAPI;
package/cjs/parse.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseVariable = void 0;
3
+ exports.parseVariable = parseVariable;
4
4
  const graphql_1 = require("graphql");
5
5
  const common_js_1 = require("./common.js");
6
6
  function parseVariable({ value, variable, schema, }) {
@@ -13,7 +13,6 @@ function parseVariable({ value, variable, schema, }) {
13
13
  schema,
14
14
  });
15
15
  }
16
- exports.parseVariable = parseVariable;
17
16
  function resolveVariable({ value, type, schema, }) {
18
17
  if (type.kind === graphql_1.Kind.NAMED_TYPE) {
19
18
  const namedType = schema.getType(type.name.value);
package/cjs/router.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createRouter = void 0;
3
+ exports.createRouter = createRouter;
4
4
  const graphql_1 = require("graphql");
5
5
  const utils_1 = require("@graphql-tools/utils");
6
6
  const ast_js_1 = require("./ast.js");
@@ -54,11 +54,19 @@ function createRouter(sofa) {
54
54
  const types = sofa.schema.getTypeMap();
55
55
  for (const typeName in types) {
56
56
  const type = types[typeName];
57
- if (((0, graphql_1.isObjectType)(type) || (0, graphql_1.isInputObjectType)(type)) &&
58
- !(0, graphql_1.isIntrospectionType)(type)) {
59
- sofa.openAPI.components.schemas[typeName] = (0, types_js_1.buildSchemaObjectFromType)(type, {
60
- customScalars: sofa.customScalars,
61
- });
57
+ if (!(0, graphql_1.isIntrospectionType)(type)) {
58
+ if (((0, graphql_1.isObjectType)(type) || (0, graphql_1.isInputObjectType)(type))) {
59
+ sofa.openAPI.components.schemas[typeName] = (0, types_js_1.buildSchemaObjectFromType)(type, {
60
+ customScalars: sofa.customScalars,
61
+ });
62
+ }
63
+ else if ((0, graphql_1.isEnumType)(type)) {
64
+ sofa.openAPI.components.schemas[typeName] = {
65
+ title: type.name,
66
+ type: 'string',
67
+ enum: type.getValues().map((value) => value.name),
68
+ };
69
+ }
62
70
  }
63
71
  }
64
72
  const router = (0, fets_1.createRouter)({
@@ -149,7 +157,6 @@ function createRouter(sofa) {
149
157
  });
150
158
  return router;
151
159
  }
152
- exports.createRouter = createRouter;
153
160
  function createQueryRoute({ sofa, router, fieldName, }) {
154
161
  logger_js_1.logger.debug(`[Router] Creating ${fieldName} query`);
155
162
  const queryType = sofa.schema.getQueryType();
@@ -203,10 +210,12 @@ function createQueryRoute({ sofa, router, fieldName, }) {
203
210
  }
204
211
  function getRouteSchemas({ method, path, info, sofa, responseStatus, }) {
205
212
  const params = {
213
+ type: 'object',
206
214
  properties: {},
207
215
  required: [],
208
216
  };
209
217
  const query = {
218
+ type: 'object',
210
219
  properties: {},
211
220
  required: [],
212
221
  };
package/cjs/sofa.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isContextFn = exports.createSofa = void 0;
3
+ exports.createSofa = createSofa;
4
+ exports.isContextFn = isContextFn;
4
5
  const graphql_1 = require("graphql");
5
6
  const common_js_1 = require("./common.js");
6
7
  const logger_js_1 = require("./logger.js");
@@ -33,11 +34,9 @@ function createSofa(config) {
33
34
  ...config,
34
35
  };
35
36
  }
36
- exports.createSofa = createSofa;
37
37
  function isContextFn(context) {
38
38
  return typeof context === 'function';
39
39
  }
40
- exports.isContextFn = isContextFn;
41
40
  // Objects and Unions are the only things that are used to define return types
42
41
  // and both might contain an ID
43
42
  // We don't treat Unions as models because
package/esm/router.js CHANGED
@@ -1,4 +1,4 @@
1
- import { isObjectType, isNonNullType, Kind, isIntrospectionType, isInputObjectType, } from 'graphql';
1
+ import { isObjectType, isNonNullType, Kind, isIntrospectionType, isInputObjectType, isEnumType, } from 'graphql';
2
2
  import { buildOperationNodeForField, createGraphQLError } from '@graphql-tools/utils';
3
3
  import { getOperationInfo } from './ast.js';
4
4
  import { convertName } from './common';
@@ -51,11 +51,19 @@ export function createRouter(sofa) {
51
51
  const types = sofa.schema.getTypeMap();
52
52
  for (const typeName in types) {
53
53
  const type = types[typeName];
54
- if ((isObjectType(type) || isInputObjectType(type)) &&
55
- !isIntrospectionType(type)) {
56
- sofa.openAPI.components.schemas[typeName] = buildSchemaObjectFromType(type, {
57
- customScalars: sofa.customScalars,
58
- });
54
+ if (!isIntrospectionType(type)) {
55
+ if ((isObjectType(type) || isInputObjectType(type))) {
56
+ sofa.openAPI.components.schemas[typeName] = buildSchemaObjectFromType(type, {
57
+ customScalars: sofa.customScalars,
58
+ });
59
+ }
60
+ else if (isEnumType(type)) {
61
+ sofa.openAPI.components.schemas[typeName] = {
62
+ title: type.name,
63
+ type: 'string',
64
+ enum: type.getValues().map((value) => value.name),
65
+ };
66
+ }
59
67
  }
60
68
  }
61
69
  const router = createRouterInstance({
@@ -199,10 +207,12 @@ function createQueryRoute({ sofa, router, fieldName, }) {
199
207
  }
200
208
  function getRouteSchemas({ method, path, info, sofa, responseStatus, }) {
201
209
  const params = {
210
+ type: 'object',
202
211
  properties: {},
203
212
  required: [],
204
213
  };
205
214
  const query = {
215
+ type: 'object',
206
216
  properties: {},
207
217
  required: [],
208
218
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sofa-api",
3
- "version": "0.18.2",
3
+ "version": "0.18.5",
4
4
  "description": "Create REST APIs with GraphQL",
5
5
  "sideEffects": false,
6
6
  "peerDependencies": {
@@ -9,7 +9,7 @@
9
9
  "dependencies": {
10
10
  "@graphql-tools/utils": "^10.0.0",
11
11
  "@whatwg-node/fetch": "^0.9.14",
12
- "fets": "^0.7.0",
12
+ "fets": "^0.8.0",
13
13
  "ansi-colors": "^4.1.3",
14
14
  "openapi-types": "^12.1.0",
15
15
  "param-case": "^3.0.4",
@@ -13,3 +13,8 @@ export type ContextFn = (serverContext: DefaultSofaServerContext) => Promise<Con
13
13
  export type DefaultSofaServerContext = {
14
14
  request: Request;
15
15
  };
16
+ export interface ObjectJSONSchema {
17
+ type: 'object';
18
+ properties: Record<string, any>;
19
+ required: string[];
20
+ }
@@ -13,3 +13,8 @@ export type ContextFn = (serverContext: DefaultSofaServerContext) => Promise<Con
13
13
  export type DefaultSofaServerContext = {
14
14
  request: Request;
15
15
  };
16
+ export interface ObjectJSONSchema {
17
+ type: 'object';
18
+ properties: Record<string, any>;
19
+ required: string[];
20
+ }