Skip to content

Commit be8a57b

Browse files
committed
Implement vite-plugin
1 parent d90402d commit be8a57b

File tree

18 files changed

+215
-74
lines changed

18 files changed

+215
-74
lines changed

bun.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"version": "0.1.0",
8080
"devDependencies": {
8181
"@types/node": "^24.10",
82+
"openapi-types": "^12.1",
8283
"typescript": "^5.9",
8384
},
8485
},
@@ -87,6 +88,7 @@
8788
"version": "0.1.0",
8889
"dependencies": {
8990
"@devup-api/core": "workspace:*",
91+
"@devup-api/generator": "workspace:*",
9092
"@devup-api/utils": "workspace:*",
9193
},
9294
"devDependencies": {

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './additional'
22
export * from './api-struct'
33
export * from './options'
4+
export * from './url-map'
45
export * from './utils'

packages/core/src/options.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@ export interface DevupApiOptions extends DevupApiTypeGeneratorOptions {
1717
* @default 'df'
1818
*/
1919
tempDir?: string
20+
21+
/**
22+
* OpenAPI file path
23+
* @default 'openapi.json'
24+
*/
25+
openapiFile?: string
2026
}

packages/core/src/url-map.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface UrlMapValue {
2+
method: 'get' | 'post' | 'put' | 'delete' | 'patch'
3+
url: string
4+
}

packages/fetch/src/api.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ export class devupApi {
2929
})
3030
}
3131

32+
GET<T extends DevupApiStructKey>(
33+
path: T,
34+
options?: RequestInit & Additional<T, DevupGetApiStruct>,
35+
) {
36+
return fetch(`${this.baseUrl}${getUrl(path)}`, {
37+
...this.defaultOptions,
38+
...options,
39+
})
40+
}
41+
3242
post<T extends DevupApiStructKey>(
3343
path: T,
3444
options?: RequestInit & Additional<T, DevupPostApiStruct>,
@@ -39,6 +49,16 @@ export class devupApi {
3949
})
4050
}
4151

52+
POST<T extends DevupApiStructKey>(
53+
path: T,
54+
options?: RequestInit & Additional<T, DevupPostApiStruct>,
55+
) {
56+
return fetch(`${this.baseUrl}${getUrl(path)}`, {
57+
...this.defaultOptions,
58+
...options,
59+
})
60+
}
61+
4262
put<T extends DevupApiStructKey>(
4363
path: T,
4464
options?: RequestInit & Additional<T, DevupPutApiStruct>,
@@ -49,6 +69,16 @@ export class devupApi {
4969
})
5070
}
5171

72+
PUT<T extends DevupApiStructKey>(
73+
path: T,
74+
options?: RequestInit & Additional<T, DevupPutApiStruct>,
75+
) {
76+
return fetch(`${this.baseUrl}${getUrl(path)}`, {
77+
...this.defaultOptions,
78+
...options,
79+
})
80+
}
81+
5282
delete<T extends DevupApiStructKey>(
5383
path: T,
5484
options?: RequestInit & Additional<T, DevupDeleteApiStruct>,
@@ -59,6 +89,16 @@ export class devupApi {
5989
})
6090
}
6191

92+
DELETE<T extends DevupApiStructKey>(
93+
path: T,
94+
options?: RequestInit & Additional<T, DevupDeleteApiStruct>,
95+
) {
96+
return fetch(`${this.baseUrl}${getUrl(path)}`, {
97+
...this.defaultOptions,
98+
...options,
99+
})
100+
}
101+
62102
patch<T extends DevupApiStructKey>(
63103
path: T,
64104
options?: RequestInit & Additional<T, DevupPatchApiStruct>,
@@ -69,6 +109,16 @@ export class devupApi {
69109
})
70110
}
71111

112+
PATCH<T extends DevupApiStructKey>(
113+
path: T,
114+
options?: RequestInit & Additional<T, DevupPatchApiStruct>,
115+
) {
116+
return fetch(`${this.baseUrl}${getUrl(path)}`, {
117+
...this.defaultOptions,
118+
...options,
119+
})
120+
}
121+
72122
request<T extends DevupApiStructKey>(
73123
path: T,
74124
options?: RequestInit & Additional<T, DevupApiStruct>,

packages/fetch/src/url-map.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
export const devup_URL_MAP: Record<
2-
string,
3-
['get' | 'post' | 'put' | 'delete' | 'patch', string]
4-
> = JSON.parse(process.env.devup_URL_MAP || '{}')
1+
import type { UrlMapValue } from '@devup-api/core'
2+
3+
export const devup_URL_MAP: Record<string, UrlMapValue> = JSON.parse(
4+
process.env.DEVUP_API_URL_MAP || '{}',
5+
)
56

67
export function getUrl(key: string): string {
7-
return devup_URL_MAP[key]?.[1] || key
8+
return devup_URL_MAP[key]?.url || key
89
}
910

10-
export function getUrlWithMethod(
11-
key: string,
12-
): ['get' | 'post' | 'put' | 'delete' | 'patch', string] {
13-
return devup_URL_MAP[key] || ['get', key]
11+
export function getUrlWithMethod(key: string): UrlMapValue {
12+
return devup_URL_MAP[key] || { method: 'get', url: key }
1413
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { DevupApiTypeGeneratorOptions, UrlMapValue } from '@devup-api/core'
2+
import type { OpenAPIV3_1 } from 'openapi-types'
3+
import { convertCase } from './convert-case'
4+
5+
export function createUrlMap(
6+
schema: OpenAPIV3_1.Document,
7+
8+
options?: DevupApiTypeGeneratorOptions,
9+
) {
10+
const convertCaseType = options?.convertCase ?? 'camel'
11+
const urlMap: Record<string, UrlMapValue> = {}
12+
for (const [path, pathItem] of Object.entries(schema.paths ?? {})) {
13+
if (!pathItem) continue
14+
for (const method of ['get', 'post', 'put', 'delete', 'patch'] as const) {
15+
const operation = pathItem[method]
16+
if (!operation) continue
17+
urlMap[convertCase(path, convertCaseType)] = { method, url: path }
18+
}
19+
}
20+
return urlMap
21+
}

packages/generator/src/generate-interface.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DevupApiTypeGeneratorOptions } from '@devup-api/core'
2-
import type { OpenAPIV3 } from 'openapi-types'
2+
import type { OpenAPIV3_1 } from 'openapi-types'
33
import { convertCase } from './convert-case'
44
import {
55
extractParameters,
@@ -15,7 +15,7 @@ interface EndpointDefinition {
1515
}
1616

1717
export function generateInterface(
18-
schema: OpenAPIV3.Document,
18+
schema: OpenAPIV3_1.Document,
1919
options?: DevupApiTypeGeneratorOptions,
2020
): string {
2121
const endpoints: Record<string, EndpointDefinition> = {}

packages/generator/src/generate-schema.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import type { OpenAPIV3 } from 'openapi-types'
1+
import type { OpenAPIV3_1 } from 'openapi-types'
22

33
/**
44
* Resolve $ref reference in OpenAPI schema
55
*/
66
export function resolveRef(
77
ref: string,
8-
document: OpenAPIV3.Document,
9-
): OpenAPIV3.SchemaObject | null {
8+
document: OpenAPIV3_1.Document,
9+
): OpenAPIV3_1.SchemaObject | null {
1010
if (!ref.startsWith('#/')) {
1111
return null
1212
}
@@ -23,7 +23,7 @@ export function resolveRef(
2323
}
2424

2525
if (current && typeof current === 'object' && !('$ref' in current)) {
26-
return current as OpenAPIV3.SchemaObject
26+
return current as OpenAPIV3_1.SchemaObject
2727
}
2828

2929
return null
@@ -33,8 +33,8 @@ export function resolveRef(
3333
* Convert OpenAPI schema to TypeScript type representation
3434
*/
3535
export function getTypeFromSchema(
36-
schema: OpenAPIV3.SchemaObject | OpenAPIV3.ReferenceObject,
37-
document: OpenAPIV3.Document,
36+
schema: OpenAPIV3_1.SchemaObject | OpenAPIV3_1.ReferenceObject,
37+
document: OpenAPIV3_1.Document,
3838
): unknown {
3939
// Handle $ref
4040
if ('$ref' in schema) {
@@ -45,7 +45,7 @@ export function getTypeFromSchema(
4545
return 'unknown'
4646
}
4747

48-
const schemaObj = schema as OpenAPIV3.SchemaObject
48+
const schemaObj = schema as OpenAPIV3_1.SchemaObject
4949

5050
// Handle allOf, anyOf, oneOf
5151
if (schemaObj.allOf) {
@@ -161,9 +161,9 @@ export function formatTypeValue(value: unknown): string {
161161
* Extract parameters from OpenAPI operation
162162
*/
163163
export function extractParameters(
164-
pathItem: OpenAPIV3.PathItemObject | undefined,
165-
operation: OpenAPIV3.OperationObject | undefined,
166-
document: OpenAPIV3.Document,
164+
pathItem: OpenAPIV3_1.PathItemObject | undefined,
165+
operation: OpenAPIV3_1.OperationObject | undefined,
166+
document: OpenAPIV3_1.Document,
167167
): {
168168
pathParams: Record<string, unknown>
169169
queryParams: Record<string, unknown>
@@ -223,10 +223,10 @@ export function extractParameters(
223223
*/
224224
export function extractRequestBody(
225225
requestBody:
226-
| OpenAPIV3.RequestBodyObject
227-
| OpenAPIV3.ReferenceObject
226+
| OpenAPIV3_1.RequestBodyObject
227+
| OpenAPIV3_1.ReferenceObject
228228
| undefined,
229-
document: OpenAPIV3.Document,
229+
document: OpenAPIV3_1.Document,
230230
): unknown {
231231
if (!requestBody) {
232232
return undefined
@@ -235,7 +235,8 @@ export function extractRequestBody(
235235
if ('$ref' in requestBody) {
236236
const resolved = resolveRef(requestBody.$ref, document)
237237
if (resolved && 'content' in resolved && resolved.content) {
238-
const content = resolved.content as OpenAPIV3.RequestBodyObject['content']
238+
const content =
239+
resolved.content as OpenAPIV3_1.RequestBodyObject['content']
239240
const jsonContent = content['application/json']
240241
if (jsonContent && 'schema' in jsonContent && jsonContent.schema) {
241242
return getTypeFromSchema(jsonContent.schema, document)

packages/generator/src/index.ts

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,2 @@
1-
/**
2-
* Type generation package
3-
*/
4-
5-
export interface TypeGeneratorOptions {
6-
outputPath?: string
7-
format?: 'typescript' | 'json'
8-
}
9-
10-
export async function generateTypes(
11-
_options?: TypeGeneratorOptions,
12-
): Promise<void> {
13-
// Type generation logic
14-
}
15-
16-
export function generateTypeFromSchema(_schema: unknown): string {
17-
// Generate type from schema
18-
return ''
19-
}
1+
export * from './create-url-map'
2+
export * from './generate-interface'

0 commit comments

Comments
 (0)