Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changepacks/changepack_log_b2zlSGpeRX5LEJNPy_XI6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"packages/fetch/package.json":"Patch","packages/generator/package.json":"Patch","packages/vite-plugin/package.json":"Patch","packages/core/package.json":"Patch","packages/next-plugin/package.json":"Patch","packages/webpack-plugin/package.json":"Patch","packages/rsbuild-plugin/package.json":"Patch"},"note":"Add method layer","date":"2026-03-06T12:51:48.129089Z"}
8 changes: 7 additions & 1 deletion packages/core/src/url-map.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'

export interface UrlMapValue {
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH'
method: HttpMethod
url: string
bodyType?: 'json' | 'form' | 'multipart'
}

export type UrlMapStoredValue = Omit<UrlMapValue, 'method'>

export type UrlMapEntry = Partial<Record<HttpMethod, UrlMapStoredValue>>
27 changes: 16 additions & 11 deletions packages/fetch/src/__tests__/api-body-type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,28 @@ import { afterEach, beforeEach, describe, expect, mock, test } from 'bun:test'
import type { UrlMapValue } from '@devup-api/core'

// Mock the url-map module to return custom bodyType values
const mockUrlMap: Record<string, Record<string, UrlMapValue>> = {
const mockUrlMap: Record<
string,
Record<string, Partial<Record<string, Omit<UrlMapValue, 'method'>>>>
> = {
'openapi.json': {
submitForm: { method: 'POST', url: '/submit', bodyType: 'form' },
uploadFile: { method: 'POST', url: '/upload', bodyType: 'multipart' },
jsonEndpoint: { method: 'POST', url: '/json', bodyType: 'json' },
submitForm: { POST: { url: '/submit', bodyType: 'form' } },
uploadFile: {
POST: { url: '/upload', bodyType: 'multipart' },
},
jsonEndpoint: { POST: { url: '/json', bodyType: 'json' } },
},
}

mock.module('../url-map', () => ({
DEVUP_API_URL_MAP: mockUrlMap,
getApiEndpointInfo: (key: string, serverName: string): UrlMapValue => {
const result = mockUrlMap[serverName]?.[key] ?? {
method: 'GET' as const,
url: key,
}
result.url ||= key
return result
getApiEndpointInfo: (
key: string,
serverName: string,
method: string,
): UrlMapValue => {
const stored = mockUrlMap[serverName]?.[key]?.[method]
return { method: method as 'GET', url: key, ...stored }
},
}))

Expand Down
53 changes: 32 additions & 21 deletions packages/fetch/src/__tests__/url-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { beforeEach, expect, test } from 'bun:test'

const urlMap = {
foo: {
getUsers: { method: 'GET' as const, url: '/users' },
createUser: { method: 'POST' as const, url: '/users' },
updateUser: { method: 'PUT' as const, url: '/users/{id}' },
deleteUser: { method: 'DELETE' as const, url: '/users/{id}' },
getUsers: { GET: { url: '/users' } },
createUser: { POST: { url: '/users' } },
updateUser: { PUT: { url: '/users/{id}' } },
deleteUser: { DELETE: { url: '/users/{id}' } },
},
}

Expand All @@ -15,41 +15,52 @@ beforeEach(() => {
const random = Math.random()

test.each([
['getUsers', '/users', JSON.stringify(urlMap)],
['createUser', '/users', JSON.stringify(urlMap)],
['updateUser', '/users/{id}', JSON.stringify(urlMap)],
['deleteUser', '/users/{id}', JSON.stringify(urlMap)],
] as const)('getApiEndpointInfo returns url for existing key: %s -> %s', async (key, expected, envValue) => {
['getUsers', '/users', 'GET', JSON.stringify(urlMap)],
['createUser', '/users', 'POST', JSON.stringify(urlMap)],
['updateUser', '/users/{id}', 'PUT', JSON.stringify(urlMap)],
['deleteUser', '/users/{id}', 'DELETE', JSON.stringify(urlMap)],
] as const)('getApiEndpointInfo returns url for existing key: %s -> %s', async (key, expected, method, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
// Add query parameter to bypass module cache and reload
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo')?.url).toBe(expected)
expect(getApiEndpointInfo(key, 'foo', method)?.url).toBe(expected)
})

test.each([
['nonExistentKey', 'nonExistentKey', JSON.stringify(urlMap)],
['unknown', 'unknown', JSON.stringify(urlMap)],
['', '', JSON.stringify(urlMap)],
['/users', '/users', JSON.stringify(urlMap)],
] as const)('getApiEndpointInfo returns key itself when key does not exist: %s -> %s', async (key, expected, envValue) => {
['nonExistentKey', 'nonExistentKey', 'GET', JSON.stringify(urlMap)],
['unknown', 'unknown', 'GET', JSON.stringify(urlMap)],
['', '', 'GET', JSON.stringify(urlMap)],
['/users', '/users', 'GET', JSON.stringify(urlMap)],
] as const)('getApiEndpointInfo returns key itself when key does not exist: %s -> %s', async (key, expected, method, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo').url).toBe(expected)
expect(getApiEndpointInfo(key, 'foo', method).url).toBe(expected)
})

test.each([
['getUsers', { method: 'GET', url: '/users' }, JSON.stringify(urlMap)],
['createUser', { method: 'POST', url: '/users' }, JSON.stringify(urlMap)],
['updateUser', { method: 'PUT', url: '/users/{id}' }, JSON.stringify(urlMap)],
['getUsers', { method: 'GET', url: '/users' }, 'GET', JSON.stringify(urlMap)],
[
'createUser',
{ method: 'POST', url: '/users' },
'POST',
JSON.stringify(urlMap),
],
[
'updateUser',
{ method: 'PUT', url: '/users/{id}' },
'PUT',
JSON.stringify(urlMap),
],
[
'deleteUser',
{ method: 'DELETE', url: '/users/{id}' },
'DELETE',
JSON.stringify(urlMap),
],
] as const)('getApiEndpointInfo returns UrlMapValue for existing key: %s -> %s', async (key, expected, envValue) => {
] as const)('getApiEndpointInfo returns UrlMapValue for existing key: %s -> %s', async (key, expected, method, envValue) => {
process.env.DEVUP_API_URL_MAP = envValue
const { getApiEndpointInfo } = await import(`../url-map?t=${random}`)
expect(getApiEndpointInfo(key, 'foo')).toEqual(expected)
expect(getApiEndpointInfo(key, 'foo', method)).toEqual(expected)
})

test.each([
Expand Down
14 changes: 5 additions & 9 deletions packages/fetch/src/url-map.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import type { UrlMapValue } from '@devup-api/core'
import type { HttpMethod, UrlMapEntry, UrlMapValue } from '@devup-api/core'

export const DEVUP_API_URL_MAP: Record<
string,
Record<string, UrlMapValue>
Record<string, UrlMapEntry>
> = JSON.parse(process.env.DEVUP_API_URL_MAP || '{}')

export function getApiEndpointInfo(
key: string,
serverName: string,
method: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH',
method: HttpMethod,
): UrlMapValue {
const result = DEVUP_API_URL_MAP[serverName]?.[key] ?? {
method,
url: key,
}
result.url ||= key
return result
const stored = DEVUP_API_URL_MAP[serverName]?.[key]?.[method]
return { method, url: key, ...stored }
}
Loading