@stacksjs/api 0.65.0 → 0.67.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/dist/index.js +110 -92
- package/package.json +4 -6
- package/dist/index.js.map +0 -652
- package/src/generate-openapi.ts +0 -73
- package/src/index.ts +0 -2
- package/src/ofetch.ts +0 -122
package/src/generate-openapi.ts
DELETED
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
import { path } from '@stacksjs/path'
|
|
2
|
-
import { route } from '@stacksjs/router'
|
|
3
|
-
|
|
4
|
-
export async function generateOpenApi(): Promise<void> {
|
|
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
|
-
routeLists.forEach((route) => {
|
|
19
|
-
const path = route.url.replace(/\{([^}]+)\}/g, '{$1}')
|
|
20
|
-
if (!openAPISpec.paths[path]) {
|
|
21
|
-
openAPISpec.paths[path] = {}
|
|
22
|
-
}
|
|
23
|
-
openAPISpec.paths[path][route.method.toLowerCase()] = {
|
|
24
|
-
summary: route.name,
|
|
25
|
-
operationId: route.callback,
|
|
26
|
-
parameters: route.paramNames.map((param: string) => ({
|
|
27
|
-
name: param,
|
|
28
|
-
in: 'path',
|
|
29
|
-
required: true,
|
|
30
|
-
schema: {
|
|
31
|
-
type: 'string',
|
|
32
|
-
},
|
|
33
|
-
})),
|
|
34
|
-
responses: {
|
|
35
|
-
[route.statusCode]: {
|
|
36
|
-
description: 'Successful response',
|
|
37
|
-
content: {
|
|
38
|
-
'application/json': {
|
|
39
|
-
schema: {
|
|
40
|
-
type: 'object',
|
|
41
|
-
properties: {},
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
},
|
|
46
|
-
},
|
|
47
|
-
}
|
|
48
|
-
// Add schemas to components if they are not already defined
|
|
49
|
-
if (route.responseSchema) {
|
|
50
|
-
const schemaName = `${route.name.replace(/\./g, '')}Response`
|
|
51
|
-
openAPISpec.components.schemas[schemaName] = route.responseSchema
|
|
52
|
-
openAPISpec.paths[path][route.method.toLowerCase()].responses[route.statusCode].content[
|
|
53
|
-
'application/json'
|
|
54
|
-
].schema = {
|
|
55
|
-
$ref: `#/components/schemas/${schemaName}`,
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (route.requestSchema) {
|
|
59
|
-
const schemaName = `${route.name.replace(/\./g, '')}Request`
|
|
60
|
-
openAPISpec.components.schemas[schemaName] = route.requestSchema
|
|
61
|
-
openAPISpec.paths[path][route.method.toLowerCase()].requestBody.content['application/json'].schema = {
|
|
62
|
-
$ref: `#/components/schemas/${schemaName}`,
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
const file = Bun.file(path.frameworkPath(`api/openapi.json`))
|
|
68
|
-
const writer = file.writer()
|
|
69
|
-
|
|
70
|
-
writer.write(JSON.stringify(openAPISpec))
|
|
71
|
-
|
|
72
|
-
await writer.end()
|
|
73
|
-
}
|
package/src/index.ts
DELETED
package/src/ofetch.ts
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
import { ofetch } from 'ofetch'
|
|
2
|
-
|
|
3
|
-
interface Params {
|
|
4
|
-
[key: string]: any // Replace 'any' with more specific types if possible
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
interface ApiFetch {
|
|
8
|
-
get: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>
|
|
9
|
-
post: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>
|
|
10
|
-
destroy: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>
|
|
11
|
-
patch: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>
|
|
12
|
-
put: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>
|
|
13
|
-
setToken: (authToken: string) => void
|
|
14
|
-
baseURL: '/' | string
|
|
15
|
-
loading: boolean
|
|
16
|
-
token: string
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
type FetchResponse = string | Blob | ArrayBuffer | ReadableStream<Uint8Array>
|
|
20
|
-
|
|
21
|
-
let loading = false
|
|
22
|
-
let token = ''
|
|
23
|
-
const baseURL = '/'
|
|
24
|
-
|
|
25
|
-
async function get(url: string, params?: Params, headers?: Headers): Promise<FetchResponse> {
|
|
26
|
-
if (headers) {
|
|
27
|
-
if (token)
|
|
28
|
-
headers.set('Authorization', `Bearer ${token}`)
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return await ofetch(url, { method: 'GET', baseURL, params, headers })
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async function post(url: string, params?: Params, headers?: Headers): Promise<any> {
|
|
35
|
-
if (headers) {
|
|
36
|
-
if (token)
|
|
37
|
-
headers.set('Authorization', `Bearer ${token}`)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
loading = true
|
|
41
|
-
|
|
42
|
-
try {
|
|
43
|
-
const result: string | FetchResponse | Blob | ArrayBuffer | ReadableStream<Uint8Array> = await ofetch(url, {
|
|
44
|
-
method: 'POST',
|
|
45
|
-
baseURL,
|
|
46
|
-
params,
|
|
47
|
-
headers,
|
|
48
|
-
})
|
|
49
|
-
|
|
50
|
-
loading = false
|
|
51
|
-
return result
|
|
52
|
-
}
|
|
53
|
-
catch (err: any) {
|
|
54
|
-
loading = false
|
|
55
|
-
|
|
56
|
-
throw err
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
async function patch(url: string, params?: Params, headers?: Headers): Promise<FetchResponse> {
|
|
61
|
-
if (headers) {
|
|
62
|
-
if (token)
|
|
63
|
-
headers.set('Authorization', `Bearer ${token}`)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
loading = true
|
|
67
|
-
|
|
68
|
-
return await ofetch(url, {
|
|
69
|
-
method: 'PATCH',
|
|
70
|
-
baseURL,
|
|
71
|
-
params,
|
|
72
|
-
headers,
|
|
73
|
-
})
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
async function put(url: string, params?: Params, headers?: Headers): Promise<FetchResponse> {
|
|
77
|
-
if (headers) {
|
|
78
|
-
if (token)
|
|
79
|
-
headers.set('Authorization', `Bearer ${token}`)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
loading = true
|
|
83
|
-
|
|
84
|
-
return await ofetch(url, {
|
|
85
|
-
method: 'PUT',
|
|
86
|
-
baseURL,
|
|
87
|
-
params,
|
|
88
|
-
headers,
|
|
89
|
-
})
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
async function destroy(url: string, params?: Params, headers?: Headers): Promise<FetchResponse> {
|
|
93
|
-
if (headers) {
|
|
94
|
-
if (token)
|
|
95
|
-
headers.set('Authorization', `Bearer ${token}`)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
loading = true
|
|
99
|
-
|
|
100
|
-
return await ofetch(url, {
|
|
101
|
-
method: 'DELETE',
|
|
102
|
-
baseURL,
|
|
103
|
-
params,
|
|
104
|
-
headers,
|
|
105
|
-
})
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function setToken(authToken: string): void {
|
|
109
|
-
token = authToken
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
export const Fetch: ApiFetch = {
|
|
113
|
-
get,
|
|
114
|
-
post,
|
|
115
|
-
patch,
|
|
116
|
-
put,
|
|
117
|
-
destroy,
|
|
118
|
-
baseURL,
|
|
119
|
-
token,
|
|
120
|
-
setToken,
|
|
121
|
-
loading,
|
|
122
|
-
}
|