@nocobase/plugin-api-doc 0.13.0-alpha.1

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.
Files changed (59) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +80 -0
  3. package/README.zh-CN.md +79 -0
  4. package/client.d.ts +2 -0
  5. package/client.js +1 -0
  6. package/dist/client/Document.d.ts +4 -0
  7. package/dist/client/index.d.ts +6 -0
  8. package/dist/client/index.js +48583 -0
  9. package/dist/index.d.ts +1 -0
  10. package/dist/index.js +11 -0
  11. package/dist/locale/en-US.d.ts +2 -0
  12. package/dist/locale/en-US.js +5 -0
  13. package/dist/locale/index.d.ts +4 -0
  14. package/dist/locale/index.js +20 -0
  15. package/dist/locale/zh-CN.d.ts +6 -0
  16. package/dist/locale/zh-CN.js +9 -0
  17. package/dist/server/index.d.ts +1 -0
  18. package/dist/server/index.js +11 -0
  19. package/dist/server/server.d.ts +8 -0
  20. package/dist/server/server.js +51 -0
  21. package/dist/server/swagger/base-swagger.d.ts +49 -0
  22. package/dist/server/swagger/base-swagger.js +54 -0
  23. package/dist/server/swagger/collections/components/field-type-map.d.ts +22 -0
  24. package/dist/server/swagger/collections/components/field-type-map.js +34 -0
  25. package/dist/server/swagger/collections/components/index.d.ts +24 -0
  26. package/dist/server/swagger/collections/components/index.js +18 -0
  27. package/dist/server/swagger/collections/components/parameters.d.ts +5 -0
  28. package/dist/server/swagger/collections/components/parameters.js +173 -0
  29. package/dist/server/swagger/collections/components/schemas.d.ts +23 -0
  30. package/dist/server/swagger/collections/components/schemas.js +91 -0
  31. package/dist/server/swagger/collections/index.d.ts +31 -0
  32. package/dist/server/swagger/collections/index.js +22 -0
  33. package/dist/server/swagger/collections/paths/associations/index.d.ts +6 -0
  34. package/dist/server/swagger/collections/paths/associations/index.js +32 -0
  35. package/dist/server/swagger/collections/paths/associations/multiple-association.d.ts +6 -0
  36. package/dist/server/swagger/collections/paths/associations/multiple-association.js +106 -0
  37. package/dist/server/swagger/collections/paths/associations/single-association.d.ts +5 -0
  38. package/dist/server/swagger/collections/paths/associations/single-association.js +88 -0
  39. package/dist/server/swagger/collections/paths/collection.d.ts +201 -0
  40. package/dist/server/swagger/collections/paths/collection.js +333 -0
  41. package/dist/server/swagger/collections/paths/index.d.ts +4 -0
  42. package/dist/server/swagger/collections/paths/index.js +30 -0
  43. package/dist/server/swagger/collections/tags/index.d.ts +6 -0
  44. package/dist/server/swagger/collections/tags/index.js +26 -0
  45. package/dist/server/swagger/constants.d.ts +10 -0
  46. package/dist/server/swagger/constants.js +14 -0
  47. package/dist/server/swagger/helpers.d.ts +171 -0
  48. package/dist/server/swagger/helpers.js +111 -0
  49. package/dist/server/swagger/index.d.ts +48 -0
  50. package/dist/server/swagger/index.js +221 -0
  51. package/dist/server/swagger/loader.d.ts +4 -0
  52. package/dist/server/swagger/loader.js +54 -0
  53. package/dist/server/swagger/merge.d.ts +1 -0
  54. package/dist/server/swagger/merge.js +13 -0
  55. package/dist/swagger.d.ts +43 -0
  56. package/dist/swagger.js +46 -0
  57. package/package.json +24 -0
  58. package/server.d.ts +3 -0
  59. package/server.js +1 -0
@@ -0,0 +1 @@
1
+ export { default } from './server';
package/dist/index.js ADDED
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ var server = require('./server');
4
+
5
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
+
7
+ var server__default = /*#__PURE__*/_interopDefault(server);
8
+
9
+
10
+
11
+ module.exports = server__default.default;
@@ -0,0 +1,2 @@
1
+ declare const _default: {};
2
+ export default _default;
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ var en_US_default = {};
4
+
5
+ module.exports = en_US_default;
@@ -0,0 +1,4 @@
1
+ export declare const NAMESPACE = "api-doc";
2
+ export declare function lang(key: string): string;
3
+ export declare function generateNTemplate(key: string): string;
4
+ export declare function useTranslation(): import("react-i18next").UseTranslationResponse<"api-doc", undefined>;
@@ -0,0 +1,20 @@
1
+ 'use strict';
2
+
3
+ var client = require('@nocobase/client');
4
+ var reactI18next = require('react-i18next');
5
+
6
+ const NAMESPACE = "api-doc";
7
+ function lang(key) {
8
+ return client.i18n.t(key, { ns: NAMESPACE });
9
+ }
10
+ function generateNTemplate(key) {
11
+ return `{{t('${key}', { ns: '${NAMESPACE}' })}}`;
12
+ }
13
+ function useTranslation() {
14
+ return reactI18next.useTranslation(NAMESPACE);
15
+ }
16
+
17
+ exports.NAMESPACE = NAMESPACE;
18
+ exports.generateNTemplate = generateNTemplate;
19
+ exports.lang = lang;
20
+ exports.useTranslation = useTranslation;
@@ -0,0 +1,6 @@
1
+ declare const _default: {
2
+ 'API documentation': string;
3
+ Documentation: string;
4
+ 'Select a definition': string;
5
+ };
6
+ export default _default;
@@ -0,0 +1,9 @@
1
+ 'use strict';
2
+
3
+ var zh_CN_default = {
4
+ "API documentation": "Api \u6587\u6863",
5
+ "Documentation": "\u6587\u6863",
6
+ "Select a definition": "\u9009\u62E9\u7AEF\u70B9"
7
+ };
8
+
9
+ module.exports = zh_CN_default;
@@ -0,0 +1 @@
1
+ export { default } from './server';
@@ -0,0 +1,11 @@
1
+ 'use strict';
2
+
3
+ var server = require('./server');
4
+
5
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
6
+
7
+ var server__default = /*#__PURE__*/_interopDefault(server);
8
+
9
+
10
+
11
+ module.exports = server__default.default;
@@ -0,0 +1,8 @@
1
+ import { Plugin } from '@nocobase/server';
2
+ import { SwaggerManager } from './swagger';
3
+ export default class APIDoc extends Plugin {
4
+ swagger: SwaggerManager;
5
+ constructor(app: any, options: any);
6
+ beforeLoad(): void;
7
+ load(): Promise<void>;
8
+ }
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ var server = require('@nocobase/server');
4
+ var swagger = require('./swagger');
5
+
6
+ class APIDoc extends server.Plugin {
7
+ swagger;
8
+ constructor(app, options) {
9
+ super(app, options);
10
+ this.swagger = new swagger.SwaggerManager(this);
11
+ }
12
+ beforeLoad() {
13
+ }
14
+ async load() {
15
+ this.app.resource({
16
+ name: "swagger",
17
+ type: "single",
18
+ actions: {
19
+ getUrls: async (ctx, next) => {
20
+ ctx.body = await this.swagger.getUrls();
21
+ await next();
22
+ },
23
+ get: async (ctx, next) => {
24
+ ctx.withoutDataWrapping = true;
25
+ const { ns } = ctx.action.params;
26
+ if (!ns) {
27
+ ctx.body = await this.swagger.getSwagger();
28
+ return;
29
+ }
30
+ const [type, index] = ns.split("/");
31
+ if (type === "core") {
32
+ ctx.body = await this.swagger.getCoreSwagger();
33
+ } else if (type === "plugins") {
34
+ ctx.body = await this.swagger.getPluginsSwagger(index);
35
+ } else if (type === "collections") {
36
+ ctx.body = await this.swagger.getCollectionsSwagger(index);
37
+ }
38
+ await next();
39
+ }
40
+ },
41
+ only: ["get", "getUrls"]
42
+ });
43
+ this.app.acl.allow("swagger", ["get", "getUrls"], "loggedIn");
44
+ this.app.acl.registerSnippet({
45
+ name: ["pm", this.name, "documentation"].join("."),
46
+ actions: ["swagger:*"]
47
+ });
48
+ }
49
+ }
50
+
51
+ module.exports = APIDoc;
@@ -0,0 +1,49 @@
1
+ declare const _default: {
2
+ openapi: string;
3
+ info: {
4
+ title: string;
5
+ description: string;
6
+ contact: {
7
+ url: string;
8
+ };
9
+ license: {
10
+ name: string;
11
+ url: string;
12
+ };
13
+ };
14
+ externalDocs: {
15
+ description: string;
16
+ url: string;
17
+ };
18
+ components: {
19
+ securitySchemes: {
20
+ 'api-key': {
21
+ type: string;
22
+ scheme: string;
23
+ };
24
+ };
25
+ schemas: {
26
+ error: {
27
+ type: string;
28
+ properties: {
29
+ errors: {
30
+ type: string;
31
+ items: {
32
+ type: string;
33
+ properties: {
34
+ message: {
35
+ description: string;
36
+ type: string;
37
+ };
38
+ };
39
+ };
40
+ };
41
+ };
42
+ };
43
+ };
44
+ };
45
+ security: {
46
+ 'api-key': any[];
47
+ }[];
48
+ };
49
+ export default _default;
@@ -0,0 +1,54 @@
1
+ 'use strict';
2
+
3
+ var base_swagger_default = {
4
+ openapi: "3.0.3",
5
+ info: {
6
+ title: "NocoBase API documentation",
7
+ description: "",
8
+ contact: {
9
+ url: "https://github.com/nocobase/nocobase/issues"
10
+ },
11
+ license: {
12
+ name: "Core packages are Apache 2.0 & Plugins packages are AGPL 3.0 licensed.",
13
+ url: "https://github.com/nocobase/nocobase#license"
14
+ }
15
+ },
16
+ externalDocs: {
17
+ description: "Find out more about NocoBase",
18
+ url: "https://docs.nocobase.com/api/http"
19
+ },
20
+ components: {
21
+ securitySchemes: {
22
+ "api-key": {
23
+ type: "http",
24
+ scheme: "bearer"
25
+ }
26
+ },
27
+ schemas: {
28
+ error: {
29
+ type: "object",
30
+ properties: {
31
+ errors: {
32
+ type: "array",
33
+ items: {
34
+ type: "object",
35
+ properties: {
36
+ message: {
37
+ description: "\u9519\u8BEF\u4FE1\u606F",
38
+ type: "string"
39
+ }
40
+ }
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
46
+ },
47
+ security: [
48
+ {
49
+ "api-key": []
50
+ }
51
+ ]
52
+ };
53
+
54
+ module.exports = base_swagger_default;
@@ -0,0 +1,22 @@
1
+ import { Field } from '@nocobase/database';
2
+ declare const fieldTypeMap: {
3
+ bigint: {
4
+ type: string;
5
+ format: string;
6
+ };
7
+ datetime: {
8
+ type: string;
9
+ format: string;
10
+ };
11
+ string: {
12
+ type: string;
13
+ };
14
+ text: {
15
+ type: string;
16
+ };
17
+ jsontype: {
18
+ type: string;
19
+ };
20
+ };
21
+ declare function getTypeByField(field: Field): any;
22
+ export { fieldTypeMap, getTypeByField };
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ const fieldTypeMap = {
4
+ bigint: {
5
+ type: "integer",
6
+ format: "int64"
7
+ },
8
+ datetime: {
9
+ type: "string",
10
+ format: "date-time"
11
+ },
12
+ string: {
13
+ type: "string"
14
+ },
15
+ text: {
16
+ type: "string"
17
+ },
18
+ jsontype: {
19
+ type: "string"
20
+ }
21
+ };
22
+ function getTypeByField(field) {
23
+ const fieldType = field.dataType.toString().toLowerCase();
24
+ const fieldAttributes = fieldTypeMap[fieldType];
25
+ if (!fieldAttributes) {
26
+ return {
27
+ type: "string"
28
+ };
29
+ }
30
+ return fieldAttributes;
31
+ }
32
+
33
+ exports.fieldTypeMap = fieldTypeMap;
34
+ exports.getTypeByField = getTypeByField;
@@ -0,0 +1,24 @@
1
+ import { Collection } from '@nocobase/database';
2
+ declare const _default: (collection: Collection, options: any) => {
3
+ parameters: {};
4
+ schemas: {
5
+ [x: string]: {
6
+ type: string;
7
+ properties: {};
8
+ allOf?: undefined;
9
+ } | {
10
+ allOf: ({
11
+ $ref: string;
12
+ type?: undefined;
13
+ properties?: undefined;
14
+ } | {
15
+ type: string;
16
+ properties: any;
17
+ $ref?: undefined;
18
+ })[];
19
+ type?: undefined;
20
+ properties?: undefined;
21
+ };
22
+ };
23
+ };
24
+ export default _default;
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ var schemas = require('./schemas');
4
+ var parameters = require('./parameters');
5
+
6
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
7
+
8
+ var schemas__default = /*#__PURE__*/_interopDefault(schemas);
9
+ var parameters__default = /*#__PURE__*/_interopDefault(parameters);
10
+
11
+ var components_default = (collection, options) => {
12
+ return {
13
+ ...schemas__default.default(collection, options),
14
+ ...parameters__default.default(collection)
15
+ };
16
+ };
17
+
18
+ module.exports = components_default;
@@ -0,0 +1,5 @@
1
+ import { Collection } from '@nocobase/database';
2
+ declare const _default: (collection: Collection) => {
3
+ parameters: {};
4
+ };
5
+ export default _default;
@@ -0,0 +1,173 @@
1
+ 'use strict';
2
+
3
+ var fieldTypeMap = require('./field-type-map');
4
+
5
+ var parameters_default = (collection) => {
6
+ const primaryKey = collection.model.primaryKeyAttribute;
7
+ const parameters = {};
8
+ if (primaryKey) {
9
+ const primaryKeyField = collection.fields.get(primaryKey);
10
+ if (!primaryKeyField) {
11
+ throw new Error(`primaryKeyField not found: ${primaryKey}, ${collection.name}`);
12
+ }
13
+ Object.assign(parameters, {
14
+ collectionIndex: {
15
+ required: true,
16
+ name: "collectionIndex",
17
+ in: "path",
18
+ description: "collection index",
19
+ schema: fieldTypeMap.getTypeByField(collection.fields.get(primaryKey))
20
+ },
21
+ filterByTk: {
22
+ name: "filterByTk",
23
+ in: "query",
24
+ description: "filter by TK(default by ID)",
25
+ schema: fieldTypeMap.getTypeByField(collection.fields.get(primaryKey))
26
+ },
27
+ filterByTks: {
28
+ name: "filterByTk",
29
+ in: "query",
30
+ description: "filter by TKs(default by ID), example: `1,2,3`",
31
+ schema: {
32
+ type: "array",
33
+ items: fieldTypeMap.getTypeByField(collection.fields.get(primaryKey))
34
+ }
35
+ }
36
+ });
37
+ }
38
+ Object.assign(parameters, {
39
+ filter: {
40
+ name: "filter",
41
+ in: "query",
42
+ description: "filter items",
43
+ schema: {
44
+ type: "object"
45
+ }
46
+ },
47
+ sort: {
48
+ name: "sort",
49
+ in: "query",
50
+ description: "sort items by fields, example: `-field1,-field2,field3`",
51
+ schema: {
52
+ oneOf: [
53
+ {
54
+ type: "array",
55
+ items: {
56
+ type: "string"
57
+ },
58
+ example: ["-id", "createdAt"]
59
+ },
60
+ {
61
+ type: "string",
62
+ example: "-id,createdAt"
63
+ }
64
+ ]
65
+ }
66
+ },
67
+ fields: {
68
+ name: "fields",
69
+ in: "query",
70
+ description: "select fields, example: `field1,field2`",
71
+ schema: {
72
+ oneOf: [
73
+ {
74
+ type: "array",
75
+ items: {
76
+ type: "string"
77
+ },
78
+ example: ["id", "createdAt"]
79
+ },
80
+ {
81
+ type: "string",
82
+ example: "id,createdAt"
83
+ }
84
+ ]
85
+ }
86
+ },
87
+ except: {
88
+ name: "except",
89
+ in: "query",
90
+ description: "except fields in results, example: `field1,field2`",
91
+ schema: {
92
+ oneOf: [
93
+ {
94
+ type: "array",
95
+ items: {
96
+ type: "string"
97
+ },
98
+ example: ["id", "createdAt"]
99
+ },
100
+ {
101
+ type: "string",
102
+ example: "id,createdAt"
103
+ }
104
+ ]
105
+ }
106
+ },
107
+ appends: {
108
+ name: "appends",
109
+ in: "query",
110
+ description: "append associations in results, example: `assoc1,assoc2`",
111
+ schema: {
112
+ oneOf: [
113
+ {
114
+ type: "array",
115
+ items: {
116
+ type: "string"
117
+ },
118
+ example: ["id", "createdAt"]
119
+ },
120
+ {
121
+ type: "string",
122
+ example: "id,createdAt"
123
+ }
124
+ ]
125
+ }
126
+ },
127
+ whitelist: {
128
+ name: "whitelist",
129
+ in: "query",
130
+ description: "whitelist for fields changes, example: `field1,field2`",
131
+ schema: {
132
+ oneOf: [
133
+ {
134
+ type: "array",
135
+ items: {
136
+ type: "string"
137
+ },
138
+ example: ["id", "createdAt"]
139
+ },
140
+ {
141
+ type: "string",
142
+ example: "id,createdAt"
143
+ }
144
+ ]
145
+ }
146
+ },
147
+ blacklist: {
148
+ name: "blacklist",
149
+ in: "query",
150
+ description: "blacklist for fields changes, example: `field1,field2`",
151
+ schema: {
152
+ oneOf: [
153
+ {
154
+ type: "array",
155
+ items: {
156
+ type: "string"
157
+ },
158
+ example: ["id", "createdAt"]
159
+ },
160
+ {
161
+ type: "string",
162
+ example: "id,createdAt"
163
+ }
164
+ ]
165
+ }
166
+ }
167
+ });
168
+ return {
169
+ parameters
170
+ };
171
+ };
172
+
173
+ module.exports = parameters_default;
@@ -0,0 +1,23 @@
1
+ import { Collection } from '@nocobase/database';
2
+ declare const _default: (collection: Collection, options: any) => {
3
+ schemas: {
4
+ [x: string]: {
5
+ type: string;
6
+ properties: {};
7
+ allOf?: undefined;
8
+ } | {
9
+ allOf: ({
10
+ $ref: string;
11
+ type?: undefined;
12
+ properties?: undefined;
13
+ } | {
14
+ type: string;
15
+ properties: any;
16
+ $ref?: undefined;
17
+ })[];
18
+ type?: undefined;
19
+ properties?: undefined;
20
+ };
21
+ };
22
+ };
23
+ export default _default;
@@ -0,0 +1,91 @@
1
+ 'use strict';
2
+
3
+ var database = require('@nocobase/database');
4
+ var fieldTypeMap = require('./field-type-map');
5
+ var associations = require('../paths/associations');
6
+
7
+ function getCollectionReadOnlyFields(collection) {
8
+ var _a;
9
+ const readOnlyFields = [];
10
+ const primaryKey = collection.model.primaryKeyAttribute;
11
+ if (primaryKey) {
12
+ const primaryField = collection.fields.get(primaryKey);
13
+ if (primaryField && ((_a = primaryField.options) == null ? void 0 : _a.autoIncrement)) {
14
+ readOnlyFields.push(primaryKey);
15
+ }
16
+ }
17
+ for (const [fieldName, field] of collection.fields) {
18
+ if (field.type == "sort") {
19
+ readOnlyFields.push(fieldName);
20
+ continue;
21
+ }
22
+ if (field.type == "context") {
23
+ readOnlyFields.push(fieldName);
24
+ continue;
25
+ }
26
+ if ([
27
+ "createdAt",
28
+ "updatedAt",
29
+ "created_at",
30
+ "updated_at",
31
+ "sort",
32
+ "created_by_id",
33
+ "createdById",
34
+ "updatedById",
35
+ "updated_by_id"
36
+ ].includes(fieldName)) {
37
+ readOnlyFields.push(fieldName);
38
+ continue;
39
+ }
40
+ }
41
+ return readOnlyFields;
42
+ }
43
+ function collectionToSchema(collection) {
44
+ return {
45
+ [collection.name]: {
46
+ type: "object",
47
+ properties: Array.from(collection.fields).filter(([key, value]) => {
48
+ return !(value instanceof database.RelationField);
49
+ }).reduce((obj, [key, value]) => {
50
+ obj[key] = {
51
+ ...getFieldTypeAttributes(value)
52
+ };
53
+ return obj;
54
+ }, {})
55
+ },
56
+ [`${collection.name}.form`]: {
57
+ allOf: [
58
+ {
59
+ $ref: `#/components/schemas/${collection.name}`
60
+ },
61
+ {
62
+ type: "object",
63
+ properties: getCollectionReadOnlyFields(collection).reduce((obj, key) => {
64
+ obj[key] = {
65
+ readOnly: true
66
+ };
67
+ return obj;
68
+ }, {})
69
+ }
70
+ ]
71
+ }
72
+ };
73
+ }
74
+ var schemas_default = (collection, options) => {
75
+ const associations$1 = associations.associationFields(collection);
76
+ const associationsTarget = associations$1.map((field) => collection.db.getCollection(field.target));
77
+ const schemas = collectionToSchema(collection);
78
+ if (options.withAssociation) {
79
+ for (const target of associationsTarget) {
80
+ Object.assign(schemas, collectionToSchema(target));
81
+ }
82
+ }
83
+ return {
84
+ schemas
85
+ };
86
+ };
87
+ function getFieldTypeAttributes(field) {
88
+ return fieldTypeMap.getTypeByField(field);
89
+ }
90
+
91
+ module.exports = schemas_default;
@@ -0,0 +1,31 @@
1
+ import { Collection } from '@nocobase/database';
2
+ declare function collectionToSwaggerObject(collection: Collection, options: any): {
3
+ paths: any;
4
+ components: {
5
+ parameters: {};
6
+ schemas: {
7
+ [x: string]: {
8
+ type: string;
9
+ properties: {};
10
+ allOf?: undefined;
11
+ } | {
12
+ allOf: ({
13
+ $ref: string;
14
+ type?: undefined;
15
+ properties?: undefined;
16
+ } | {
17
+ type: string;
18
+ properties: any;
19
+ $ref?: undefined;
20
+ })[];
21
+ type?: undefined;
22
+ properties?: undefined;
23
+ };
24
+ };
25
+ };
26
+ tags: {
27
+ name: string;
28
+ description: any;
29
+ }[];
30
+ };
31
+ export default collectionToSwaggerObject;