sessioin-and-cookie example

This commit is contained in:
King Wang
2021-06-14 17:45:11 +08:00
parent 5af32e67ba
commit 54c2f31f83
28 changed files with 297 additions and 233 deletions

View File

@@ -1,19 +1,13 @@
import { HttpClient } from "tsrpc-browser";
const CookieStorageKey = '__MY_COOKIE__';
const SessionStorageKey = '__MY_SESSION__';
export function enableSessionAndCookie(client: HttpClient<any>) {
export function enableCookie(client: HttpClient<any>) {
// Send
client.flows.preCallApiFlow.push(v => {
// Auto get __cookie from localStorage
let cookieStr = localStorage.getItem(CookieStorageKey);
v.req.__cookie = cookieStr ? JSON.parse(cookieStr) : undefined;
// Auto get __session from sessionStorage
let sessionStr = sessionStorage.getItem(SessionStorageKey);
v.req.__session = sessionStr ? JSON.parse(sessionStr) : undefined;
return v;
})
@@ -24,10 +18,6 @@ export function enableSessionAndCookie(client: HttpClient<any>) {
if (v.return.res.__cookie) {
localStorage.setItem(CookieStorageKey, JSON.stringify(v.return.res.__cookie))
}
// Auto set __session to sessionStorage
if (v.return.res.__session) {
sessionStorage.setItem(SessionStorageKey, JSON.stringify(v.return.res.__session))
}
}
return v;

View File

@@ -1,7 +1,13 @@
import { HttpClient } from "tsrpc-browser";
export function showReqAndRes(client: HttpClient<any>) {
// Send
// Clear when send
client.flows.preCallApiFlow.push(v => {
document.querySelector('.apiReq')!.innerHTML = '';
document.querySelector('.apiReturn')!.innerHTML = '';
return v;
});
// Ouput when recv
client.flows.postApiReturnFlow.push(v => {
(document.querySelector('.apiReq') as HTMLElement).innerText = 'callApi: ' + v.apiName + '\n\n' + JSON.stringify(v.req, null, 2);
(document.querySelector('.apiReturn') as HTMLElement).innerText = JSON.stringify(v.return, null, 2);