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
3 changes: 3 additions & 0 deletions packages/core/playground/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export const userApi = loadingPlugin({
getUser: typedUrl<User, { id: string | number }>`/user/${(params: { id: string }) => params.id}`,
addUser: typedUrl<User, any, Partial<Omit<User, 'id'>>>`post /user`,
delUser: typedUrl<never, { id: string | number }>`delete /user/${'id'}`,
dynamic: typedUrl<any, { id: string | number }>({
method: 'get',
})``,
},
'http://localhost:7009',
),
Expand Down
8 changes: 8 additions & 0 deletions packages/core/playground/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export default defineComponent({

onMounted(() => {
getUsers();

userApi
.dynamic({
url: '/user/1',
})
.then((res) => {
console.log('dynamic', res);
});
});

return () => {
Expand Down
37 changes: 25 additions & 12 deletions packages/core/src/core/registApi.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { trailingSlash } from '@ace-util/core';
import { trailingSlash, isAbsoluteUrl } from '@ace-util/core';
import queryString, { type StringifyOptions } from 'query-string';

// Types
Expand Down Expand Up @@ -207,17 +207,30 @@ function transfromToRequest(
methodConfig = methodPath;
}

// 置换 方法 和 地址
// example:
// 'getUser' => 'get getUser'
// 'post updateUser'
const methodConfigArr = methodConfig.split(' ');
const [method, urlPath] = methodConfigArr.length === 1 ? ['get', methodConfig] : methodConfigArr;
// 拼接 prefix (处理prefix 末尾以及 urlPath 开始 / 的重复)
const prefixStr = typeof prefix === 'function' ? prefix(urlPath) : prefix || '';
const url = trailingSlash(prefixStr) + (urlPath.startsWith('/') ? urlPath.substring(1) : urlPath);
requestConfig.url = url;
requestConfig.method = method as Method;
if (methodConfig) {
// Fixed url
// example:
// 'getUser' => 'get getUser'
// 'post updateUser'
const methodConfigArr = methodConfig.split(' ');
const [method, urlPath] = methodConfigArr.length === 1 ? ['get', methodConfig] : methodConfigArr;
// 拼接 prefix (处理prefix 末尾以及 urlPath 开始 / 的重复)
const prefixStr = typeof prefix === 'function' ? prefix(urlPath) : prefix || '';
const url = isAbsoluteUrl(urlPath)
? urlPath
: trailingSlash(prefixStr) + (urlPath.startsWith('/') ? urlPath.substring(1) : urlPath);
requestConfig.url = url;
requestConfig.method = method as Method;
} else if (config.url) {
// Dynamic url
const urlPath = config.url;
// 拼接 prefix (处理prefix 末尾以及 urlPath 开始 / 的重复)
const prefixStr = typeof prefix === 'function' ? prefix(urlPath) : prefix || '';
const url = isAbsoluteUrl(urlPath)
? urlPath
: trailingSlash(prefixStr) + (urlPath.startsWith('/') ? urlPath.substring(1) : urlPath);
requestConfig.url = url;
}

if (!requestType) requestType = 'json';
if (!dataSerializer) dataSerializer = defaultDataSerializer;
Expand Down
Loading