@stacksjs/api 0.61.22 → 0.62.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.
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@stacksjs/api",
3
3
  "type": "module",
4
- "version": "0.61.22",
4
+ "version": "0.62.0",
5
5
  "description": "The Stacks array utilities.",
6
6
  "author": "Chris Breuer",
7
+ "contributors": [
8
+ "Chris Breuer <chris@stacksjs.org>"
9
+ ],
7
10
  "license": "MIT",
8
11
  "funding": "https://github.com/sponsors/chrisbbreuer",
9
12
  "homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/arrays#readme",
@@ -35,9 +38,6 @@
35
38
  },
36
39
  "module": "dist/index.js",
37
40
  "types": "dist/index.d.ts",
38
- "contributors": [
39
- "Chris Breuer <chris@stacksjs.org>"
40
- ],
41
41
  "files": [
42
42
  "README.md",
43
43
  "dist",
@@ -45,6 +45,7 @@
45
45
  ],
46
46
  "scripts": {
47
47
  "build": "bun --bun build.ts",
48
+ "generate-types": "openapi-typescript ./../../api/openapi.json --output ./../../api/api-types.ts",
48
49
  "typecheck": "bun --bun tsc --noEmit",
49
50
  "prepublishOnly": "bun run build"
50
51
  },
@@ -53,7 +54,8 @@
53
54
  },
54
55
  "dependencies": {
55
56
  "@stacksjs/utils": "latest",
56
- "ofetch": "^1.3.4"
57
+ "ofetch": "^1.3.4",
58
+ "openapi-typescript": "^7.0.2"
57
59
  },
58
60
  "devDependencies": {
59
61
  "@stacksjs/development": "latest"
@@ -0,0 +1,82 @@
1
+ import { path } from '@stacksjs/path'
2
+ import { route } from '@stacksjs/router'
3
+
4
+ async function generateOpenAPISpec() {
5
+ const routeLists: any[] = await route.getRoutes()
6
+
7
+ const openAPISpec = {
8
+ openapi: '3.0.0',
9
+ info: {
10
+ title: 'Generated API',
11
+ version: '1.0.0',
12
+ },
13
+ paths: {} as Record<string, any>,
14
+ components: {
15
+ schemas: {} as Record<string, any>,
16
+ },
17
+ }
18
+
19
+ routeLists.forEach((route) => {
20
+ const path = route.url.replace(/{([^}]+)}/g, '{$1}')
21
+ if (!openAPISpec.paths[path]) {
22
+ openAPISpec.paths[path] = {}
23
+ }
24
+
25
+ openAPISpec.paths[path][route.method.toLowerCase()] = {
26
+ summary: route.name,
27
+ operationId: route.callback,
28
+ parameters: route.paramNames.map((param) => ({
29
+ name: param,
30
+ in: 'path',
31
+ required: true,
32
+ schema: {
33
+ type: 'string',
34
+ },
35
+ })),
36
+ responses: {
37
+ [route.statusCode]: {
38
+ description: 'Successful response',
39
+ content: {
40
+ 'application/json': {
41
+ schema: {
42
+ type: 'object',
43
+ properties: {},
44
+ },
45
+ },
46
+ },
47
+ },
48
+ },
49
+ }
50
+
51
+ // Add schemas to components if they are not already defined
52
+ if (route.responseSchema) {
53
+ const schemaName = `${route.name.replace(/\./g, '')}Response`
54
+ openAPISpec.components.schemas[schemaName] = route.responseSchema
55
+ openAPISpec.paths[path][route.method.toLowerCase()].responses[route.statusCode].content[
56
+ 'application/json'
57
+ ].schema = {
58
+ $ref: `#/components/schemas/${schemaName}`,
59
+ }
60
+ }
61
+
62
+ if (route.requestSchema) {
63
+ const schemaName = `${route.name.replace(/\./g, '')}Request`
64
+ openAPISpec.components.schemas[schemaName] = route.requestSchema
65
+ openAPISpec.paths[path][route.method.toLowerCase()].requestBody.content['application/json'].schema = {
66
+ $ref: `#/components/schemas/${schemaName}`,
67
+ }
68
+ }
69
+ })
70
+
71
+ return openAPISpec
72
+ }
73
+
74
+ const openAPISpec = await generateOpenAPISpec()
75
+
76
+ const file = Bun.file(path.projectStoragePath(`framework/api/openapi.json`))
77
+
78
+ const writer = file.writer()
79
+
80
+ writer.write(JSON.stringify(openAPISpec.toString()))
81
+
82
+ await writer.end()