-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
641 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { | ||
setHandler, | ||
setSocketIOInstance, | ||
socketIOHandler, | ||
setXiorInstance, | ||
xiorHandler, | ||
getHandler, | ||
} from 'fe-sdk-demo'; | ||
import { QueryTodoRes } from 'fe-sdk-demo/lib/apiconf-refs'; | ||
import { TodoStatus } from 'fe-sdk-demo/lib/modules/todo/Todo.entity'; | ||
import { AddTodo, QueryTodo } from 'fe-sdk-demo/lib/user-api'; | ||
import Head from 'next/head'; | ||
import { useState, useEffect } from 'react'; | ||
// eslint-disable-next-line import/namespace | ||
import { io as SocketIO } from 'socket.io-client'; | ||
import xior from 'xior'; | ||
|
||
import styles from '../../styles/Home.module.css'; | ||
|
||
const baseURL = | ||
// process.env.NODE_ENV === 'production' | ||
// ? process.env.BASE_URL | ||
// : | ||
(() => { | ||
if (typeof window === 'undefined') return; | ||
return ( | ||
window?.location.protocol + '//' + window?.location.host.split(':')[0] + ':' + 3012 + '/' | ||
); | ||
})(); | ||
|
||
const apiType = 'user'; | ||
const socketURL = baseURL; | ||
const apiURL = baseURL + `api/${apiType}`; | ||
|
||
export default function Home() { | ||
const [handlerName, setHanlderName] = useState(''); | ||
const [result, setResult] = useState<QueryTodoRes>(); | ||
|
||
useEffect(() => { | ||
const io = SocketIO(socketURL, { | ||
transports: ['websocket'], | ||
query: { | ||
type: apiType, | ||
}, | ||
}); | ||
setSocketIOInstance(io); | ||
setHandler(socketIOHandler); | ||
|
||
setHanlderName(getHandler().name); | ||
|
||
io.on('connect', async () => { | ||
await AddTodo({ | ||
status: TodoStatus.todo, | ||
title: 'create by socket.io', | ||
}); | ||
const wsRes = await QueryTodo({}); | ||
|
||
console.log(wsRes); | ||
setResult(wsRes); | ||
|
||
io.disconnect(); | ||
setTimeout(async () => { | ||
setXiorInstance(xior.create({ baseURL: apiURL })); | ||
setHandler(xiorHandler); | ||
|
||
setHanlderName(getHandler().name); | ||
await AddTodo({ | ||
status: TodoStatus.todo, | ||
title: 'create by axios', | ||
}); | ||
const httpRes = await QueryTodo({}); | ||
console.log(httpRes); | ||
|
||
setResult(httpRes); | ||
}, 2500); | ||
}); | ||
|
||
return () => { | ||
io.off('connect'); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<Head> | ||
<title>[web] Expo monorepo</title> | ||
<meta name="description" content="Sharing code with Next.js" /> | ||
<link rel="icon" href="/favicon.ico" /> | ||
</Head> | ||
|
||
<main className={styles.main}> | ||
Hello, {handlerName}: {JSON.stringify(result)} | ||
</main> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './axios'; | ||
export * from './socket.io'; | ||
export * from './gen-api'; | ||
export * from './xior'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { XiorRequestConfig as _XiorRequestConfig, xior } from 'xior'; | ||
|
||
import { NoHandlerError } from './error'; | ||
import { pathParams } from './path-params'; | ||
import { APIConfig, checkMethodHasBody, trimAndRemoveUndefined } from './shared/tsdk-helper'; | ||
|
||
let xiorInstance: xior; | ||
|
||
/** | ||
* Set the XiorInstance | ||
* | ||
* @param instance - XiorInstance | ||
*/ | ||
export const setXiorInstance = (instance: xior): void => { | ||
xiorInstance = instance; | ||
}; | ||
|
||
/** | ||
* Get the XiorInstance | ||
* | ||
* @param instance - XiorInstance | ||
* @returns The XiorInstance | ||
*/ | ||
export const getXiorInstance = () => { | ||
return xiorInstance; | ||
}; | ||
|
||
export type XiorRequestConfig<ReqPayload> = Omit<_XiorRequestConfig, 'data'> & { | ||
data?: ReqPayload; | ||
}; | ||
|
||
export async function xiorHandler( | ||
apiConfig: APIConfig, | ||
requestData: any, | ||
requestConfig?: XiorRequestConfig<any>, | ||
/** remove fields with undefined */ | ||
needTrim?: boolean | ||
) { | ||
const xiorInstance = getXiorInstance(); | ||
if (!xiorInstance) { | ||
throw new NoHandlerError(`Call \`setXiorInstance\` first`); | ||
} | ||
const { path, headers } = apiConfig; | ||
const method = apiConfig.method.toLowerCase(); | ||
|
||
// TODO: remove & {url: string} | ||
const payload: _XiorRequestConfig & { url: string } = { | ||
method: method === 'patch' ? method.toUpperCase() : method, | ||
url: path, | ||
...requestConfig, | ||
}; | ||
|
||
if (headers) { | ||
payload.headers = { | ||
...payload.headers, | ||
...headers, | ||
}; | ||
} | ||
|
||
if (requestData) { | ||
const data = needTrim ? trimAndRemoveUndefined(requestData) : requestData; | ||
if (checkMethodHasBody(method)) { | ||
payload.data = data; | ||
if (requestConfig?.params) { | ||
payload.params = requestConfig.params; | ||
} | ||
} else { | ||
payload.params = requestConfig?.params ? { ...requestConfig.params, ...data } : data; | ||
} | ||
} | ||
if (requestData && (apiConfig as any).paramsInUrl) { | ||
payload.url = pathParams(path, requestData, (apiConfig as any).paramsInUrl); | ||
} | ||
|
||
const { data } = await xiorInstance.request(payload); | ||
return data; | ||
} |
Oops, something went wrong.