Skip to content

Commit

Permalink
feat: remove sdk's needTrim and bump xior to v0.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
suhaotian committed Apr 29, 2024
1 parent c062bc1 commit 4039270
Show file tree
Hide file tree
Showing 14 changed files with 10,219 additions and 9,797 deletions.
5 changes: 3 additions & 2 deletions examples/server/packages/fe-sdk-demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"zod": "^3",
"axios": "^1.6.7",
"xior": "^0.3.12",
"xior": "^0.4.1",
"change-case": "^4.1.2",
"kysely": "^0.26.3",
"@tanstack/react-query": "^5.22.2",
Expand All @@ -31,6 +31,7 @@
}
},
"files": [
"lib"
"lib",
"src"
]
}
19 changes: 0 additions & 19 deletions examples/server/src/shared/tsdk-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,6 @@ export function isObject<T = any>(data: T) {
return result;
}

/**
* remove undefined field or trim string value
*
* @param data - the object data
*/
export const trimAndRemoveUndefined = (data: ObjectLiteral): ObjectLiteral => {
if (!isObject(data)) return data;
const newData: ObjectLiteral = {};
Object.keys(data).forEach((k: string) => {
const valueType = typeof data[k];
if (valueType === 'string') {
newData[k] = data[k].trim();
} else if (valueType !== 'undefined') {
newData[k] = data[k];
}
});
return newData;
};

export const ProtocolTypes = {
request: 'REQ:',
response: 'RES:',
Expand Down
2 changes: 1 addition & 1 deletion examples/server/tsdk.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ module.exports = {
dependencies: {
i18next: '^23.10.1',
'intl-pluralrules': '^2.0.1',
xior: '^0.3.12',
xior: '^0.4.1',
},
};
2 changes: 1 addition & 1 deletion examples/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"react-native-web": "^0.19.6",
"socket.io-client": "^4.7.5",
"axios": "^1.6.7",
"xior": "^0.3.12"
"xior": "^0.4.1"
},
"devDependencies": {
"@configs/vitest-config": "workspace:*",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "tsdk-monorepo",
"packageManager": "pnpm@8.11.0",
"packageManager": "pnpm@9.0.6",
"scripts": {
"lint": "turbo lint",
"checktype": "turbo checktype",
Expand Down Expand Up @@ -37,7 +37,7 @@
"overrides": {
"uuid": "^3.4.0",
"@babel/traverse": ">=7.23.2",
"xior": "^0.3.12"
"xior": "^0.4.1"
}
},
"eslintConfig": {
Expand Down
19 changes: 0 additions & 19 deletions packages/bench/src/shared/tsdk-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,6 @@ export function isObject<T = any>(data: T) {
return result;
}

/**
* remove undefined field or trim string value
*
* @param data - the object data
*/
export const trimAndRemoveUndefined = (data: ObjectLiteral): ObjectLiteral => {
if (!isObject(data)) return data;
const newData: ObjectLiteral = {};
Object.keys(data).forEach((k: string) => {
const valueType = typeof data[k];
if (valueType === 'string') {
newData[k] = data[k].trim();
} else if (valueType !== 'undefined') {
newData[k] = data[k];
}
});
return newData;
};

export const ProtocolTypes = {
request: 'REQ:',
response: 'RES:',
Expand Down
5 changes: 3 additions & 2 deletions packages/tsdk/fe-sdk-template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dependencies": {
"zod": "^3",
"axios": "^1.6.7",
"xior": "^0.3.12",
"xior": "^0.4.1",
"change-case": "^4.1.2"
},
"devDependencies": {
Expand All @@ -27,6 +27,7 @@
}
},
"files": [
"lib"
"lib",
"src"
]
}
8 changes: 3 additions & 5 deletions packages/tsdk/fe-sdk-template/src/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AxiosRequestConfig, AxiosInstance } from 'axios';

import { NoHandlerError } from './error';
import { pathParams } from './path-params';
import { APIConfig, checkMethodHasBody, trimAndRemoveUndefined } from './shared/tsdk-helper';
import { APIConfig, checkMethodHasBody } from './shared/tsdk-helper';

let axiosInstance: AxiosInstance;

Expand Down Expand Up @@ -32,9 +32,7 @@ export type RequestConfig<ReqPayload> = Omit<AxiosRequestConfig, 'data'> & {
export async function axiosHandler(
apiConfig: APIConfig,
requestData: any,
requestConfig?: RequestConfig<any>,
/** remove fields with undefined */
needTrim?: boolean
requestConfig?: RequestConfig<any>
) {
const axiosInstance = getAxiosInstance();
if (!axiosInstance) {
Expand All @@ -57,7 +55,7 @@ export async function axiosHandler(
}

if (requestData) {
const data = needTrim ? trimAndRemoveUndefined(requestData) : requestData;
const data = requestData;
if (checkMethodHasBody(method)) {
payload.data = data;
if (requestConfig?.params) {
Expand Down
16 changes: 4 additions & 12 deletions packages/tsdk/fe-sdk-template/src/gen-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ type RequestConfig<T> = AxiosRequestConfig<T>;
let handler = (
apiConfig: APIConfig,
requestData: any,
requestConfig?: any | RequestConfig<any> | ObjectLiteral | XiorRequestConfig<any>,
needTrim?: boolean
requestConfig?: any | RequestConfig<any> | ObjectLiteral | XiorRequestConfig<any>
): Promise<any> => {
return Promise.reject(new NoHandlerError(`Call \`setHandler\` first`));
};
Expand Down Expand Up @@ -46,21 +45,14 @@ export function getHandler() {
export default function genAPICall<ReqPayload, ResData>(
apiConfig: APIConfig
): {
(
data: ReqPayload,
requestConfig?: RequestConfig<ReqPayload> | ObjectLiteral,
/** remove fields with undefined */
needTrim?: boolean
): Promise<ResData>;
(data: ReqPayload, requestConfig?: RequestConfig<ReqPayload> | ObjectLiteral): Promise<ResData>;
config: APIConfig;
} {
function APICall(
data: ReqPayload,
requestConfig?: RequestConfig<ReqPayload> | ObjectLiteral,
/** remove fields with undefined */
needTrim = true
requestConfig?: RequestConfig<ReqPayload> | ObjectLiteral
): Promise<ResData> {
return getHandler()(apiConfig, data, requestConfig, needTrim);
return getHandler()(apiConfig, data, requestConfig);
}

// now you can access the config with api
Expand Down
19 changes: 0 additions & 19 deletions packages/tsdk/fe-sdk-template/src/shared/tsdk-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,6 @@ export function isObject<T = any>(data: T) {
return result;
}

/**
* remove undefined field or trim string value
*
* @param data - the object data
*/
export const trimAndRemoveUndefined = (data: ObjectLiteral): ObjectLiteral => {
if (!isObject(data)) return data;
const newData: ObjectLiteral = {};
Object.keys(data).forEach((k: string) => {
const valueType = typeof data[k];
if (valueType === 'string') {
newData[k] = data[k].trim();
} else if (valueType !== 'undefined') {
newData[k] = data[k];
}
});
return newData;
};

export const ProtocolTypes = {
request: 'REQ:',
response: 'RES:',
Expand Down
12 changes: 3 additions & 9 deletions packages/tsdk/fe-sdk-template/src/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
import type { Socket } from 'socket.io-client';

import { NoConnectionError, NoHandlerError, TimeoutError } from './error';
import {
APIConfig,
ObjectLiteral,
ProtocolTypes,
trimAndRemoveUndefined,
} from './shared/tsdk-helper';
import { APIConfig, ObjectLiteral, ProtocolTypes } from './shared/tsdk-helper';
import { getID } from './utils';

let socketIOInstance: Socket;
Expand Down Expand Up @@ -68,8 +63,7 @@ export const getSocketIOInstance: () => Socket = () => {
export function socketIOHandler(
apiConfig: APIConfig,
data: any,
requestConfig?: ObjectLiteral & { timeout?: number },
needTrim?: boolean
requestConfig?: ObjectLiteral & { timeout?: number }
): Promise<any> {
const ioInstance = getSocketIOInstance();
if (!ioInstance) {
Expand All @@ -84,7 +78,7 @@ export function socketIOHandler(

ioInstance.emit(ProtocolTypes.request, {
_id: msgId,
payload: needTrim && data ? trimAndRemoveUndefined(data) : data,
payload: data,
});

const timer = requestConfig?.timeout
Expand Down
8 changes: 3 additions & 5 deletions packages/tsdk/fe-sdk-template/src/xior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { XiorRequestConfig as _XiorRequestConfig, xior } from 'xior';

import { NoHandlerError } from './error';
import { pathParams } from './path-params';
import { APIConfig, checkMethodHasBody, trimAndRemoveUndefined } from './shared/tsdk-helper';
import { APIConfig, checkMethodHasBody } from './shared/tsdk-helper';

let xiorInstance: xior;

Expand Down Expand Up @@ -32,9 +32,7 @@ export type XiorRequestConfig<ReqPayload> = Omit<_XiorRequestConfig, 'data'> & {
export async function xiorHandler(
apiConfig: APIConfig,
requestData: any,
requestConfig?: XiorRequestConfig<any>,
/** remove fields with undefined */
needTrim?: boolean
requestConfig?: XiorRequestConfig<any>
) {
const xiorInstance = getXiorInstance();
if (!xiorInstance) {
Expand All @@ -58,7 +56,7 @@ export async function xiorHandler(
}

if (requestData) {
const data = needTrim ? trimAndRemoveUndefined(requestData) : requestData;
const data = requestData;
if (checkMethodHasBody(method)) {
payload.data = data;
if (requestConfig?.params) {
Expand Down
14 changes: 5 additions & 9 deletions packages/tsdk/src/sync-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,13 @@ export async function syncAPI() {
export function use${name}(
payload: ${name}Req | undefined,
options?: SWRConfiguration<${name}Res | undefined>,
requestConfig?: AxiosRequestConfig<${name}Req>,
needTrim?: boolean
requestConfig?: AxiosRequestConfig<${name}Req>
) {
return useSWR(
() => ({ url: ${name}.config.path, arg: payload }),
({ arg }) => {
if (typeof arg === 'undefined') return undefined;
return ${name}(arg, requestConfig, needTrim);
return ${name}(arg, requestConfig);
},
options
);
Expand All @@ -202,12 +201,11 @@ return useSWR(
${name}Req
>,
requestConfig?: AxiosRequestConfig<${name}Req>,
needTrim?: boolean
) {
return useSWRMutation(
${name}.config.path,
(url, { arg }: { arg: ${name}Req }) => {
return ${name}(arg, requestConfig, needTrim);
return ${name}(arg, requestConfig);
},
options
);
Expand All @@ -230,7 +228,6 @@ return useSWR(
options?: UndefinedInitialDataOptions<${name}Res | undefined, Error>,
queryClient?: QueryClient,
requestConfig?: AxiosRequestConfig<${name}Req>,
needTrim?: boolean
) {
return useQuery(
{
Expand All @@ -240,7 +237,7 @@ return useSWR(
if (typeof payload === 'undefined') {
return undefined;
}
return ${name}(payload, requestConfig, needTrim);
return ${name}(payload, requestConfig);
},
},
queryClient || _queryClient
Expand All @@ -261,13 +258,12 @@ return useSWR(
>,
queryClient?: QueryClient,
requestConfig?: AxiosRequestConfig<${name}Req>,
needTrim?: boolean
) {
return useMutation(
{
...(options || {}),
mutationFn(payload) {
return ${name}(payload, requestConfig, needTrim);
return ${name}(payload, requestConfig);
},
},
queryClient || _queryClient
Expand Down
Loading

0 comments on commit 4039270

Please sign in to comment.