@stacksjs/api 0.64.6 → 0.65.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.
@@ -0,0 +1,17 @@
1
+ interface Params {
2
+ [key: string]: any;
3
+ }
4
+ interface ApiFetch {
5
+ get: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>;
6
+ post: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>;
7
+ destroy: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>;
8
+ patch: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>;
9
+ put: (url: string, params?: Params, header?: Headers) => Promise<FetchResponse>;
10
+ setToken: (authToken: string) => void;
11
+ baseURL: "/" | string;
12
+ loading: boolean;
13
+ token: string;
14
+ }
15
+ type FetchResponse = string | Blob | ArrayBuffer | ReadableStream<Uint8Array>;
16
+ export declare const Fetch: ApiFetch;
17
+ export {};
package/package.json CHANGED
@@ -1,12 +1,10 @@
1
1
  {
2
2
  "name": "@stacksjs/api",
3
3
  "type": "module",
4
- "version": "0.64.6",
4
+ "version": "0.65.0",
5
5
  "description": "The Stacks array utilities.",
6
6
  "author": "Chris Breuer",
7
- "contributors": [
8
- "Chris Breuer <chris@stacksjs.org>"
9
- ],
7
+ "contributors": ["Chris Breuer <chris@stacksjs.org>"],
10
8
  "license": "MIT",
11
9
  "funding": "https://github.com/sponsors/chrisbbreuer",
12
10
  "homepage": "https://github.com/stacksjs/stacks/tree/main/storage/framework/core/arrays#readme",
@@ -38,23 +36,19 @@
38
36
  },
39
37
  "module": "dist/index.js",
40
38
  "types": "dist/index.d.ts",
41
- "files": [
42
- "README.md",
43
- "dist",
44
- "src"
45
- ],
39
+ "files": ["README.md", "dist", "src"],
46
40
  "scripts": {
47
- "build": "bun --bun build.ts",
41
+ "build": "bun build.ts",
48
42
  "generate-types": "openapi-typescript ./../../api/openapi.json --output ./../../api/api-types.ts",
49
- "typecheck": "bun --bun tsc --noEmit",
43
+ "typecheck": "bun tsc --noEmit",
50
44
  "prepublishOnly": "bun run build"
51
45
  },
52
46
  "dependencies": {
53
- "@stacksjs/utils": "latest",
54
- "ofetch": "^1.3.4",
55
- "openapi-typescript": "^7.3.0"
47
+ "@stacksjs/utils": "0.64.6",
48
+ "ofetch": "^1.4.1",
49
+ "openapi-typescript": "^7.4.1"
56
50
  },
57
51
  "devDependencies": {
58
- "@stacksjs/development": "latest"
52
+ "@stacksjs/development": "0.64.6"
59
53
  }
60
54
  }
@@ -1,7 +1,7 @@
1
1
  import { path } from '@stacksjs/path'
2
2
  import { route } from '@stacksjs/router'
3
3
 
4
- async function generateOpenAPISpec() {
4
+ export async function generateOpenApi(): Promise<void> {
5
5
  const routeLists: any[] = await route.getRoutes()
6
6
 
7
7
  const openAPISpec = {
@@ -15,17 +15,15 @@ async function generateOpenAPISpec() {
15
15
  schemas: {} as Record<string, any>,
16
16
  },
17
17
  }
18
-
19
18
  routeLists.forEach((route) => {
20
- const path = route.url.replace(/{([^}]+)}/g, '{$1}')
19
+ const path = route.url.replace(/\{([^}]+)\}/g, '{$1}')
21
20
  if (!openAPISpec.paths[path]) {
22
21
  openAPISpec.paths[path] = {}
23
22
  }
24
-
25
23
  openAPISpec.paths[path][route.method.toLowerCase()] = {
26
24
  summary: route.name,
27
25
  operationId: route.callback,
28
- parameters: route.paramNames.map((param) => ({
26
+ parameters: route.paramNames.map((param: string) => ({
29
27
  name: param,
30
28
  in: 'path',
31
29
  required: true,
@@ -47,7 +45,6 @@ async function generateOpenAPISpec() {
47
45
  },
48
46
  },
49
47
  }
50
-
51
48
  // Add schemas to components if they are not already defined
52
49
  if (route.responseSchema) {
53
50
  const schemaName = `${route.name.replace(/\./g, '')}Response`
@@ -58,7 +55,6 @@ async function generateOpenAPISpec() {
58
55
  $ref: `#/components/schemas/${schemaName}`,
59
56
  }
60
57
  }
61
-
62
58
  if (route.requestSchema) {
63
59
  const schemaName = `${route.name.replace(/\./g, '')}Request`
64
60
  openAPISpec.components.schemas[schemaName] = route.requestSchema
@@ -68,15 +64,10 @@ async function generateOpenAPISpec() {
68
64
  }
69
65
  })
70
66
 
71
- return openAPISpec
72
- }
67
+ const file = Bun.file(path.frameworkPath(`api/openapi.json`))
68
+ const writer = file.writer()
73
69
 
74
- const openAPISpec = await generateOpenAPISpec()
70
+ writer.write(JSON.stringify(openAPISpec))
75
71
 
76
- const file = Bun.file(path.frameworkPath(`api/openapi.json`))
77
-
78
- const writer = file.writer()
79
-
80
- writer.write(JSON.stringify(openAPISpec.toString()))
81
-
82
- await writer.end()
72
+ await writer.end()
73
+ }
package/src/index.ts CHANGED
@@ -1,149 +1,2 @@
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 FetchRequestHeaders {
8
- 'Content-Type'?: string
9
- Authorization?: string
10
- Accept?: string
11
- 'User-Agent'?: string
12
- Referer?: string
13
- Origin?: string
14
- 'Cache-Control'?: string
15
- Pragma?: string
16
- Cookie?: string
17
- 'If-Modified-Since'?: string
18
- 'If-None-Match'?: string
19
- 'X-Requested-With'?: string
20
- 'Accept-Encoding'?: string
21
- 'Accept-Language'?: string
22
- [key: string]: string | undefined // To allow additional headers
23
- }
24
-
25
- interface FetchRequestParams {
26
- headers?: FetchRequestHeaders
27
- method?: string
28
- baseURL?: string
29
- body?: any // Optional, for request bodies like in POST or PUT requests
30
- mode?: RequestMode // Optional, e.g., 'cors', 'no-cors', 'same-origin'
31
- credentials?: RequestCredentials // Optional, e.g., 'include', 'same-origin', 'omit'
32
- cache?: RequestCache // Optional, e.g., 'default', 'no-store', 'reload'
33
- redirect?: RequestRedirect // Optional, e.g., 'follow', 'manual', 'error'
34
- referrerPolicy?: ReferrerPolicy // Optional, e.g., 'no-referrer', 'origin'
35
- integrity?: string // Optional, for subresource integrity
36
- keepalive?: boolean // Optional, for whether the request should outlive the page
37
- signal?: AbortSignal // Optional, to abort the request
38
- [key: string]: any // To allow additional parameters
39
- }
40
-
41
- type FetchResponse = string | Blob | ArrayBuffer | ReadableStream<Uint8Array>
42
-
43
- const loading = ref(false)
44
- const token = ref('')
45
- const baseURL = '/'
46
-
47
- async function post(url: string, params?: Params): Promise<any> {
48
- const headers: FetchRequestHeaders = { Accept: 'application/json' }
49
-
50
- if (token.value) headers.Authorization = `Bearer ${token.value}`
51
-
52
- const parameters: FetchRequestParams = {
53
- ...params,
54
- ...{
55
- headers,
56
- parseResponse: JSON.parse,
57
- method: 'POST',
58
- baseURL,
59
- },
60
- }
61
-
62
- loading.value = true
63
-
64
- try {
65
- const result: string | FetchResponse | Blob | ArrayBuffer | ReadableStream<Uint8Array> = await ofetch(
66
- url,
67
- parameters,
68
- )
69
-
70
- loading.value = false
71
- return result
72
- } catch (err: any) {
73
- loading.value = false
74
-
75
- throw err
76
- }
77
- }
78
-
79
- async function get(url: string, params?: Params): Promise<any> {
80
- const headers: FetchRequestHeaders = { Accept: 'application/json' }
81
-
82
- if (token.value) headers.Authorization = `Bearer ${token.value}`
83
-
84
- const parameters: FetchRequestParams = {
85
- ...params,
86
- ...{
87
- headers,
88
- parseResponse: JSON.parse,
89
- method: 'GET',
90
- baseURL,
91
- },
92
- }
93
-
94
- return (await ofetch(url, parameters)) as FetchResponse
95
- }
96
-
97
- async function patch(url: string, params?: Params): Promise<any> {
98
- const headers: FetchRequestHeaders = { Accept: 'application/json' }
99
-
100
- if (token.value) headers.Authorization = `Bearer ${token.value}`
101
-
102
- const parameters: FetchRequestParams = {
103
- ...params,
104
- ...{
105
- headers,
106
- parseResponse: JSON.parse,
107
- method: 'PATCH',
108
- baseURL,
109
- },
110
- }
111
-
112
- loading.value = true
113
-
114
- return (await ofetch(url, parameters)) as FetchResponse
115
- }
116
-
117
- async function destroy(url: string, params?: Params): Promise<any> {
118
- const headers: FetchRequestHeaders = { Accept: 'application/json' }
119
-
120
- if (token.value) headers.Authorization = `Bearer ${token.value}`
121
-
122
- const parameters: FetchRequestParams = {
123
- ...params,
124
- ...{
125
- headers,
126
- method: 'DELETE',
127
- baseURL,
128
- },
129
- }
130
-
131
- loading.value = true
132
-
133
- return (await ofetch(url, parameters)) as FetchResponse
134
- }
135
-
136
- function setToken(authToken: string) {
137
- token.value = authToken
138
- }
139
-
140
- export const Fetch = {
141
- post,
142
- get,
143
- patch,
144
- destroy,
145
- baseURL,
146
- loading,
147
- token,
148
- setToken,
149
- }
1
+ export * from './generate-openapi'
2
+ export * from './ofetch'
package/src/ofetch.ts ADDED
@@ -0,0 +1,122 @@
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
+ }