From 54c2f31f838e2bbb24ec1cf509f4e25a5fa2dac4 Mon Sep 17 00:00:00 2001 From: King Wang Date: Mon, 14 Jun 2021 17:45:11 +0800 Subject: [PATCH] sessioin-and-cookie example --- .../session-and-cookie/backend/package.json | 4 +- .../backend/src/api/ApiClear.ts | 3 +- .../backend/src/api/ApiGetCookie.ts | 13 -- .../backend/src/api/ApiGetSession.ts | 13 -- .../backend/src/api/ApiSetCookie.ts | 14 +++ .../backend/src/api/ApiSetSession.ts | 10 ++ .../backend/src/api/ApiTest.ts | 5 +- .../session-and-cookie/backend/src/index.ts | 18 ++- .../backend/src/models/ServerSession.ts | 68 +++++++++++ .../backend/src/models/enableCookie.ts | 14 +++ .../src/shared/protocols/PtlGetCookie.ts | 9 -- .../src/shared/protocols/PtlGetSession.ts | 9 -- .../src/shared/protocols/PtlSetCookie.ts | 9 ++ .../src/shared/protocols/PtlSetSession.ts | 9 ++ .../backend/src/shared/protocols/PtlTest.ts | 5 +- .../backend/src/shared/protocols/base.ts | 11 +- .../src/shared/protocols/serviceProto.ts | 114 ++++++++---------- .../frontend/public/index.html | 4 +- .../session-and-cookie/frontend/src/index.ts | 12 +- ...bleSessionAndCookie.ts => enableCookie.ts} | 12 +- .../frontend/src/models/showReqAndRes.ts | 8 +- .../src/shared/protocols/PtlGetCookie.ts | 9 -- .../src/shared/protocols/PtlGetSession.ts | 9 -- .../src/shared/protocols/PtlSetCookie.ts | 9 ++ .../src/shared/protocols/PtlSetSession.ts | 9 ++ .../frontend/src/shared/protocols/PtlTest.ts | 5 +- .../frontend/src/shared/protocols/base.ts | 11 +- .../src/shared/protocols/serviceProto.ts | 114 ++++++++---------- 28 files changed, 297 insertions(+), 233 deletions(-) delete mode 100644 examples/session-and-cookie/backend/src/api/ApiGetCookie.ts delete mode 100644 examples/session-and-cookie/backend/src/api/ApiGetSession.ts create mode 100644 examples/session-and-cookie/backend/src/api/ApiSetCookie.ts create mode 100644 examples/session-and-cookie/backend/src/api/ApiSetSession.ts create mode 100644 examples/session-and-cookie/backend/src/models/ServerSession.ts create mode 100644 examples/session-and-cookie/backend/src/models/enableCookie.ts delete mode 100644 examples/session-and-cookie/backend/src/shared/protocols/PtlGetCookie.ts delete mode 100644 examples/session-and-cookie/backend/src/shared/protocols/PtlGetSession.ts create mode 100644 examples/session-and-cookie/backend/src/shared/protocols/PtlSetCookie.ts create mode 100644 examples/session-and-cookie/backend/src/shared/protocols/PtlSetSession.ts rename examples/session-and-cookie/frontend/src/models/{enableSessionAndCookie.ts => enableCookie.ts} (57%) delete mode 100644 examples/session-and-cookie/frontend/src/shared/protocols/PtlGetCookie.ts delete mode 100644 examples/session-and-cookie/frontend/src/shared/protocols/PtlGetSession.ts create mode 100644 examples/session-and-cookie/frontend/src/shared/protocols/PtlSetCookie.ts create mode 100644 examples/session-and-cookie/frontend/src/shared/protocols/PtlSetSession.ts diff --git a/examples/session-and-cookie/backend/package.json b/examples/session-and-cookie/backend/package.json index bb12229..580f8e4 100644 --- a/examples/session-and-cookie/backend/package.json +++ b/examples/session-and-cookie/backend/package.json @@ -12,12 +12,14 @@ }, "devDependencies": { "@types/node": "^15.12.2", + "@types/uuid": "^8.3.0", "onchange": "^7.1.0", "ts-node": "^9.1.1", "tsrpc-cli": "^2.0.1-dev.11", "typescript": "^4.3.2" }, "dependencies": { - "tsrpc": "^3.0.0-dev.19" + "tsrpc": "^3.0.0-dev.19", + "uuid": "^8.3.2" } } diff --git a/examples/session-and-cookie/backend/src/api/ApiClear.ts b/examples/session-and-cookie/backend/src/api/ApiClear.ts index d0b8e9d..f122975 100644 --- a/examples/session-and-cookie/backend/src/api/ApiClear.ts +++ b/examples/session-and-cookie/backend/src/api/ApiClear.ts @@ -3,7 +3,6 @@ import { ReqClear, ResClear } from "../shared/protocols/PtlClear"; export async function ApiClear(call: ApiCall) { call.succ({ - __cookie: {}, - __session: {} + __cookie: {} }) } \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/api/ApiGetCookie.ts b/examples/session-and-cookie/backend/src/api/ApiGetCookie.ts deleted file mode 100644 index 63f166a..0000000 --- a/examples/session-and-cookie/backend/src/api/ApiGetCookie.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { ApiCall } from "tsrpc"; -import { ReqGetCookie, ResGetCookie } from "../shared/protocols/PtlGetCookie"; - -let times = 0; - -export async function ApiGetCookie(call: ApiCall) { - call.succ({ - __cookie: { - ...call.req.__cookie, - testCookie: 'Cookie ' + (++times), - } - }) -} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/api/ApiGetSession.ts b/examples/session-and-cookie/backend/src/api/ApiGetSession.ts deleted file mode 100644 index 4e5c52f..0000000 --- a/examples/session-and-cookie/backend/src/api/ApiGetSession.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { ApiCall } from "tsrpc"; -import { ReqGetSession, ResGetSession } from "../shared/protocols/PtlGetSession"; - -let times = 0; - -export async function ApiGetSession(call: ApiCall) { - call.succ({ - __session: { - ...call.req.__session, - testSession: 'Session ' + (++times) - } - }) -} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/api/ApiSetCookie.ts b/examples/session-and-cookie/backend/src/api/ApiSetCookie.ts new file mode 100644 index 0000000..070f195 --- /dev/null +++ b/examples/session-and-cookie/backend/src/api/ApiSetCookie.ts @@ -0,0 +1,14 @@ +import { ApiCall } from "tsrpc"; +import { ReqSetCookie, ResSetCookie } from "../shared/protocols/PtlSetCookie"; + +let times = 0; + +export async function ApiSetCookie(call: ApiCall) { + ++times; + call.succ({ + __cookie: { + ...call.req.__cookie, + testCookie: 'Cookie ' + times, + } + }) +} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/api/ApiSetSession.ts b/examples/session-and-cookie/backend/src/api/ApiSetSession.ts new file mode 100644 index 0000000..3b5f8de --- /dev/null +++ b/examples/session-and-cookie/backend/src/api/ApiSetSession.ts @@ -0,0 +1,10 @@ +import { ApiCall } from "tsrpc"; +import { ReqSetSession, ResSetSession } from "../shared/protocols/PtlSetSession"; + +let times = 0; + +export async function ApiSetSession(call: ApiCall) { + ++times; + await call.setSession('testSession', 'Session ' + times); + call.succ({}) +} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/api/ApiTest.ts b/examples/session-and-cookie/backend/src/api/ApiTest.ts index 4473445..2c156b5 100644 --- a/examples/session-and-cookie/backend/src/api/ApiTest.ts +++ b/examples/session-and-cookie/backend/src/api/ApiTest.ts @@ -2,5 +2,8 @@ import { ApiCall } from "tsrpc"; import { ReqTest, ResTest } from "../shared/protocols/PtlTest"; export async function ApiTest(call: ApiCall) { - call.succ({}); + let testSession = await call.getSession('testSession'); + call.succ({ + testSession: testSession + }); } \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/index.ts b/examples/session-and-cookie/backend/src/index.ts index 5a4dbc9..3de7d43 100644 --- a/examples/session-and-cookie/backend/src/index.ts +++ b/examples/session-and-cookie/backend/src/index.ts @@ -1,6 +1,7 @@ import * as path from "path"; import { HttpServer } from "tsrpc"; -import { BaseRequest, BaseResponse } from "./shared/protocols/base"; +import { enableCookie } from "./models/enableCookie"; +import { ServerSession } from "./models/ServerSession"; import { serviceProto } from "./shared/protocols/serviceProto"; // Create the Server @@ -9,16 +10,11 @@ const server = new HttpServer(serviceProto, { cors: '*' }); -// Return session and cookie as they are, except reset by res -server.flows.preApiReturnFlow.push(v => { - if (v.return.isSucc) { - let req = v.call.req as BaseRequest; - let res = v.return.res as BaseResponse; - res.__session = res.__session ?? req.__session; - res.__cookie = res.__cookie ?? req.__cookie; - } - return v; -}); +// Enable Cookie +enableCookie(server); + +// Enable Session +new ServerSession().enable(server); // Entry function async function main() { diff --git a/examples/session-and-cookie/backend/src/models/ServerSession.ts b/examples/session-and-cookie/backend/src/models/ServerSession.ts new file mode 100644 index 0000000..ec0c6d4 --- /dev/null +++ b/examples/session-and-cookie/backend/src/models/ServerSession.ts @@ -0,0 +1,68 @@ +import { HttpServer } from "tsrpc"; +import * as uuid from "uuid"; +import { BaseRequest } from "../shared/protocols/base"; + +// This example store session data to memory for convinience. +// You can store it into database or redis as you need. +export class ServerSession { + + async enable(server: HttpServer) { + server.flows.preApiCallFlow.push(async v => { + // Init Session + let req = v.req as BaseRequest; + let { sessionId } = await this.initSession(req.__cookie?.sessionId) + req.__cookie = { + ...req.__cookie, + sessionId: sessionId + } + + // ApiCall: Get & Set Session + v.getSession = this.getSession.bind(this, sessionId); + v.setSession = this.setSession.bind(this, sessionId); + + return v; + }); + } + + // Test session storage + private _sessionData: { + [sessionId: string]: SessionData; + } = {}; + + // Storage in server memory for test + // You can modify it to storage into redis / database... + async initSession(sessionId?: string) { + // Existed sessionId + if (sessionId) { + if (this._sessionData[sessionId]) { + return { + sessionId: sessionId + } + } + } + + sessionId = uuid.v4(); + this._sessionData[sessionId] = {}; + return { + sessionId: sessionId + }; + } + async getSession(sessioinId: string, key: T): Promise { + return this._sessionData[sessioinId][key]; + } + async setSession(sessioinId: string, key: T, value: SessionData[T]): Promise { + this._sessionData[sessioinId][key] = value; + } +} + +declare module 'tsrpc' { + export interface ApiCall { + getSession: (key: T) => Promise, + setSession: (key: T, value: SessionData[T]) => Promise + } +} + +// Your custom session type definition +export interface SessionData { + testSession?: string +} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/models/enableCookie.ts b/examples/session-and-cookie/backend/src/models/enableCookie.ts new file mode 100644 index 0000000..754ac96 --- /dev/null +++ b/examples/session-and-cookie/backend/src/models/enableCookie.ts @@ -0,0 +1,14 @@ +import { HttpServer } from "tsrpc"; +import { BaseRequest, BaseResponse, Cookie } from "../shared/protocols/base"; + +export async function enableCookie(server: HttpServer) { + server.flows.preApiReturnFlow.push(v => { + if (v.return.isSucc) { + let req = v.call.req as BaseRequest; + let res = v.return.res as BaseResponse; + // Reset by API handler, or return as they are in the request + res.__cookie = res.__cookie ?? req.__cookie; + } + return v; + }); +} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/shared/protocols/PtlGetCookie.ts b/examples/session-and-cookie/backend/src/shared/protocols/PtlGetCookie.ts deleted file mode 100644 index e77630b..0000000 --- a/examples/session-and-cookie/backend/src/shared/protocols/PtlGetCookie.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { BaseRequest, BaseResponse } from './base' - -export interface ReqGetCookie extends BaseRequest { - -} - -export interface ResGetCookie extends BaseResponse { - -} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/shared/protocols/PtlGetSession.ts b/examples/session-and-cookie/backend/src/shared/protocols/PtlGetSession.ts deleted file mode 100644 index f00f4e6..0000000 --- a/examples/session-and-cookie/backend/src/shared/protocols/PtlGetSession.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { BaseRequest, BaseResponse } from './base' - -export interface ReqGetSession extends BaseRequest { - -} - -export interface ResGetSession extends BaseResponse { - -} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/shared/protocols/PtlSetCookie.ts b/examples/session-and-cookie/backend/src/shared/protocols/PtlSetCookie.ts new file mode 100644 index 0000000..394135d --- /dev/null +++ b/examples/session-and-cookie/backend/src/shared/protocols/PtlSetCookie.ts @@ -0,0 +1,9 @@ +import { BaseRequest, BaseResponse } from './base' + +export interface ReqSetCookie extends BaseRequest { + +} + +export interface ResSetCookie extends BaseResponse { + +} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/shared/protocols/PtlSetSession.ts b/examples/session-and-cookie/backend/src/shared/protocols/PtlSetSession.ts new file mode 100644 index 0000000..a28ff99 --- /dev/null +++ b/examples/session-and-cookie/backend/src/shared/protocols/PtlSetSession.ts @@ -0,0 +1,9 @@ +import { BaseRequest, BaseResponse } from './base' + +export interface ReqSetSession extends BaseRequest { + +} + +export interface ResSetSession extends BaseResponse { + +} \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/shared/protocols/PtlTest.ts b/examples/session-and-cookie/backend/src/shared/protocols/PtlTest.ts index d3e5367..9bbb40d 100644 --- a/examples/session-and-cookie/backend/src/shared/protocols/PtlTest.ts +++ b/examples/session-and-cookie/backend/src/shared/protocols/PtlTest.ts @@ -1,9 +1,10 @@ -import { BaseRequest, BaseResponse } from './base' +import { BaseRequest, BaseResponse } from './base'; export interface ReqTest extends BaseRequest { } export interface ResTest extends BaseResponse { - + // From Session + testSession?: string } \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/shared/protocols/base.ts b/examples/session-and-cookie/backend/src/shared/protocols/base.ts index 12991a0..390cc0f 100644 --- a/examples/session-and-cookie/backend/src/shared/protocols/base.ts +++ b/examples/session-and-cookie/backend/src/shared/protocols/base.ts @@ -1,9 +1,12 @@ export interface BaseRequest { - __session?: { [key: string]: any }; - __cookie?: { [key: string]: any }; + __cookie?: Cookie; } export interface BaseResponse { - __session?: { [key: string]: any }; - __cookie?: { [key: string]: any }; + __cookie?: Cookie; +} + +export interface Cookie { + sessionId?: string, + [key: string]: any } \ No newline at end of file diff --git a/examples/session-and-cookie/backend/src/shared/protocols/serviceProto.ts b/examples/session-and-cookie/backend/src/shared/protocols/serviceProto.ts index c509617..6f2b855 100644 --- a/examples/session-and-cookie/backend/src/shared/protocols/serviceProto.ts +++ b/examples/session-and-cookie/backend/src/shared/protocols/serviceProto.ts @@ -1,7 +1,7 @@ import { ServiceProto } from 'tsrpc-proto'; import { ReqClear, ResClear } from './PtlClear'; -import { ReqGetCookie, ResGetCookie } from './PtlGetCookie'; -import { ReqGetSession, ResGetSession } from './PtlGetSession'; +import { ReqSetCookie, ResSetCookie } from './PtlSetCookie'; +import { ReqSetSession, ResSetSession } from './PtlSetSession'; import { ReqTest, ResTest } from './PtlTest'; export interface ServiceType { @@ -10,13 +10,13 @@ export interface ServiceType { req: ReqClear, res: ResClear }, - "GetCookie": { - req: ReqGetCookie, - res: ResGetCookie + "SetCookie": { + req: ReqSetCookie, + res: ResSetCookie }, - "GetSession": { - req: ReqGetSession, - res: ResGetSession + "SetSession": { + req: ReqSetSession, + res: ResSetSession }, "Test": { req: ReqTest, @@ -29,25 +29,24 @@ export interface ServiceType { } export const serviceProto: ServiceProto = { - "version": 3, "services": [ { - "id": 3, + "id": 0, "name": "Clear", "type": "api" }, - { - "id": 0, - "name": "GetCookie", - "type": "api" - }, { "id": 1, - "name": "GetSession", + "name": "SetCookie", "type": "api" }, { "id": 2, + "name": "SetSession", + "type": "api" + }, + { + "id": 3, "name": "Test", "type": "api" } @@ -70,34 +69,34 @@ export const serviceProto: ServiceProto = { "properties": [ { "id": 0, - "name": "__session", - "type": { - "type": "Interface", - "indexSignature": { - "keyType": "String", - "type": { - "type": "Any" - } - } - }, - "optional": true - }, - { - "id": 1, "name": "__cookie", "type": { - "type": "Interface", - "indexSignature": { - "keyType": "String", - "type": { - "type": "Any" - } - } + "type": "Reference", + "target": "base/Cookie" }, "optional": true } ] }, + "base/Cookie": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "sessionId", + "type": { + "type": "String" + }, + "optional": true + } + ], + "indexSignature": { + "keyType": "String", + "type": { + "type": "Any" + } + } + }, "PtlClear/ResClear": { "type": "Interface", "extends": [ @@ -115,35 +114,16 @@ export const serviceProto: ServiceProto = { "properties": [ { "id": 0, - "name": "__session", - "type": { - "type": "Interface", - "indexSignature": { - "keyType": "String", - "type": { - "type": "Any" - } - } - }, - "optional": true - }, - { - "id": 1, "name": "__cookie", "type": { - "type": "Interface", - "indexSignature": { - "keyType": "String", - "type": { - "type": "Any" - } - } + "type": "Reference", + "target": "base/Cookie" }, "optional": true } ] }, - "PtlGetCookie/ReqGetCookie": { + "PtlSetCookie/ReqSetCookie": { "type": "Interface", "extends": [ { @@ -155,7 +135,7 @@ export const serviceProto: ServiceProto = { } ] }, - "PtlGetCookie/ResGetCookie": { + "PtlSetCookie/ResSetCookie": { "type": "Interface", "extends": [ { @@ -167,7 +147,7 @@ export const serviceProto: ServiceProto = { } ] }, - "PtlGetSession/ReqGetSession": { + "PtlSetSession/ReqSetSession": { "type": "Interface", "extends": [ { @@ -179,7 +159,7 @@ export const serviceProto: ServiceProto = { } ] }, - "PtlGetSession/ResGetSession": { + "PtlSetSession/ResSetSession": { "type": "Interface", "extends": [ { @@ -213,6 +193,16 @@ export const serviceProto: ServiceProto = { "target": "base/BaseResponse" } } + ], + "properties": [ + { + "id": 0, + "name": "testSession", + "type": { + "type": "String" + }, + "optional": true + } ] } } diff --git a/examples/session-and-cookie/frontend/public/index.html b/examples/session-and-cookie/frontend/public/index.html index 24ee9f8..d3c4b30 100644 --- a/examples/session-and-cookie/frontend/public/index.html +++ b/examples/session-and-cookie/frontend/public/index.html @@ -15,8 +15,8 @@
- - + +

API Request

diff --git a/examples/session-and-cookie/frontend/src/index.ts b/examples/session-and-cookie/frontend/src/index.ts index 5179ae1..6303767 100644 --- a/examples/session-and-cookie/frontend/src/index.ts +++ b/examples/session-and-cookie/frontend/src/index.ts @@ -1,5 +1,5 @@ import { HttpClient } from 'tsrpc-browser'; -import { enableSessionAndCookie } from './models/enableSessionAndCookie'; +import { enableCookie } from './models/enableCookie'; import { showReqAndRes } from './models/showReqAndRes'; import { serviceProto } from './shared/protocols/serviceProto'; @@ -10,7 +10,7 @@ let client = new HttpClient(serviceProto, { }); // Session and Cookie -enableSessionAndCookie(client); +enableCookie(client); // Show Req and Return to View showReqAndRes(client); @@ -23,9 +23,9 @@ $('#btnTest').onclick = () => { $('#btnClear').onclick = () => { client.callApi('Clear', {}); }; -$('#btnGetCookie').onclick = () => { - client.callApi('GetCookie', {}); +$('#btnSetCookie').onclick = () => { + client.callApi('SetCookie', {}); }; -$('#btnGetSession').onclick = () => { - client.callApi('GetSession', {}); +$('#btnSetSession').onclick = () => { + client.callApi('SetSession', {}); }; \ No newline at end of file diff --git a/examples/session-and-cookie/frontend/src/models/enableSessionAndCookie.ts b/examples/session-and-cookie/frontend/src/models/enableCookie.ts similarity index 57% rename from examples/session-and-cookie/frontend/src/models/enableSessionAndCookie.ts rename to examples/session-and-cookie/frontend/src/models/enableCookie.ts index 0ed7f5c..7321a34 100644 --- a/examples/session-and-cookie/frontend/src/models/enableSessionAndCookie.ts +++ b/examples/session-and-cookie/frontend/src/models/enableCookie.ts @@ -1,19 +1,13 @@ import { HttpClient } from "tsrpc-browser"; const CookieStorageKey = '__MY_COOKIE__'; -const SessionStorageKey = '__MY_SESSION__'; -export function enableSessionAndCookie(client: HttpClient) { +export function enableCookie(client: HttpClient) { // 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) { 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; diff --git a/examples/session-and-cookie/frontend/src/models/showReqAndRes.ts b/examples/session-and-cookie/frontend/src/models/showReqAndRes.ts index f670996..04931cb 100644 --- a/examples/session-and-cookie/frontend/src/models/showReqAndRes.ts +++ b/examples/session-and-cookie/frontend/src/models/showReqAndRes.ts @@ -1,7 +1,13 @@ import { HttpClient } from "tsrpc-browser"; export function showReqAndRes(client: HttpClient) { - // 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); diff --git a/examples/session-and-cookie/frontend/src/shared/protocols/PtlGetCookie.ts b/examples/session-and-cookie/frontend/src/shared/protocols/PtlGetCookie.ts deleted file mode 100644 index e77630b..0000000 --- a/examples/session-and-cookie/frontend/src/shared/protocols/PtlGetCookie.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { BaseRequest, BaseResponse } from './base' - -export interface ReqGetCookie extends BaseRequest { - -} - -export interface ResGetCookie extends BaseResponse { - -} \ No newline at end of file diff --git a/examples/session-and-cookie/frontend/src/shared/protocols/PtlGetSession.ts b/examples/session-and-cookie/frontend/src/shared/protocols/PtlGetSession.ts deleted file mode 100644 index f00f4e6..0000000 --- a/examples/session-and-cookie/frontend/src/shared/protocols/PtlGetSession.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { BaseRequest, BaseResponse } from './base' - -export interface ReqGetSession extends BaseRequest { - -} - -export interface ResGetSession extends BaseResponse { - -} \ No newline at end of file diff --git a/examples/session-and-cookie/frontend/src/shared/protocols/PtlSetCookie.ts b/examples/session-and-cookie/frontend/src/shared/protocols/PtlSetCookie.ts new file mode 100644 index 0000000..394135d --- /dev/null +++ b/examples/session-and-cookie/frontend/src/shared/protocols/PtlSetCookie.ts @@ -0,0 +1,9 @@ +import { BaseRequest, BaseResponse } from './base' + +export interface ReqSetCookie extends BaseRequest { + +} + +export interface ResSetCookie extends BaseResponse { + +} \ No newline at end of file diff --git a/examples/session-and-cookie/frontend/src/shared/protocols/PtlSetSession.ts b/examples/session-and-cookie/frontend/src/shared/protocols/PtlSetSession.ts new file mode 100644 index 0000000..a28ff99 --- /dev/null +++ b/examples/session-and-cookie/frontend/src/shared/protocols/PtlSetSession.ts @@ -0,0 +1,9 @@ +import { BaseRequest, BaseResponse } from './base' + +export interface ReqSetSession extends BaseRequest { + +} + +export interface ResSetSession extends BaseResponse { + +} \ No newline at end of file diff --git a/examples/session-and-cookie/frontend/src/shared/protocols/PtlTest.ts b/examples/session-and-cookie/frontend/src/shared/protocols/PtlTest.ts index d3e5367..9bbb40d 100644 --- a/examples/session-and-cookie/frontend/src/shared/protocols/PtlTest.ts +++ b/examples/session-and-cookie/frontend/src/shared/protocols/PtlTest.ts @@ -1,9 +1,10 @@ -import { BaseRequest, BaseResponse } from './base' +import { BaseRequest, BaseResponse } from './base'; export interface ReqTest extends BaseRequest { } export interface ResTest extends BaseResponse { - + // From Session + testSession?: string } \ No newline at end of file diff --git a/examples/session-and-cookie/frontend/src/shared/protocols/base.ts b/examples/session-and-cookie/frontend/src/shared/protocols/base.ts index 12991a0..390cc0f 100644 --- a/examples/session-and-cookie/frontend/src/shared/protocols/base.ts +++ b/examples/session-and-cookie/frontend/src/shared/protocols/base.ts @@ -1,9 +1,12 @@ export interface BaseRequest { - __session?: { [key: string]: any }; - __cookie?: { [key: string]: any }; + __cookie?: Cookie; } export interface BaseResponse { - __session?: { [key: string]: any }; - __cookie?: { [key: string]: any }; + __cookie?: Cookie; +} + +export interface Cookie { + sessionId?: string, + [key: string]: any } \ No newline at end of file diff --git a/examples/session-and-cookie/frontend/src/shared/protocols/serviceProto.ts b/examples/session-and-cookie/frontend/src/shared/protocols/serviceProto.ts index c509617..6f2b855 100644 --- a/examples/session-and-cookie/frontend/src/shared/protocols/serviceProto.ts +++ b/examples/session-and-cookie/frontend/src/shared/protocols/serviceProto.ts @@ -1,7 +1,7 @@ import { ServiceProto } from 'tsrpc-proto'; import { ReqClear, ResClear } from './PtlClear'; -import { ReqGetCookie, ResGetCookie } from './PtlGetCookie'; -import { ReqGetSession, ResGetSession } from './PtlGetSession'; +import { ReqSetCookie, ResSetCookie } from './PtlSetCookie'; +import { ReqSetSession, ResSetSession } from './PtlSetSession'; import { ReqTest, ResTest } from './PtlTest'; export interface ServiceType { @@ -10,13 +10,13 @@ export interface ServiceType { req: ReqClear, res: ResClear }, - "GetCookie": { - req: ReqGetCookie, - res: ResGetCookie + "SetCookie": { + req: ReqSetCookie, + res: ResSetCookie }, - "GetSession": { - req: ReqGetSession, - res: ResGetSession + "SetSession": { + req: ReqSetSession, + res: ResSetSession }, "Test": { req: ReqTest, @@ -29,25 +29,24 @@ export interface ServiceType { } export const serviceProto: ServiceProto = { - "version": 3, "services": [ { - "id": 3, + "id": 0, "name": "Clear", "type": "api" }, - { - "id": 0, - "name": "GetCookie", - "type": "api" - }, { "id": 1, - "name": "GetSession", + "name": "SetCookie", "type": "api" }, { "id": 2, + "name": "SetSession", + "type": "api" + }, + { + "id": 3, "name": "Test", "type": "api" } @@ -70,34 +69,34 @@ export const serviceProto: ServiceProto = { "properties": [ { "id": 0, - "name": "__session", - "type": { - "type": "Interface", - "indexSignature": { - "keyType": "String", - "type": { - "type": "Any" - } - } - }, - "optional": true - }, - { - "id": 1, "name": "__cookie", "type": { - "type": "Interface", - "indexSignature": { - "keyType": "String", - "type": { - "type": "Any" - } - } + "type": "Reference", + "target": "base/Cookie" }, "optional": true } ] }, + "base/Cookie": { + "type": "Interface", + "properties": [ + { + "id": 0, + "name": "sessionId", + "type": { + "type": "String" + }, + "optional": true + } + ], + "indexSignature": { + "keyType": "String", + "type": { + "type": "Any" + } + } + }, "PtlClear/ResClear": { "type": "Interface", "extends": [ @@ -115,35 +114,16 @@ export const serviceProto: ServiceProto = { "properties": [ { "id": 0, - "name": "__session", - "type": { - "type": "Interface", - "indexSignature": { - "keyType": "String", - "type": { - "type": "Any" - } - } - }, - "optional": true - }, - { - "id": 1, "name": "__cookie", "type": { - "type": "Interface", - "indexSignature": { - "keyType": "String", - "type": { - "type": "Any" - } - } + "type": "Reference", + "target": "base/Cookie" }, "optional": true } ] }, - "PtlGetCookie/ReqGetCookie": { + "PtlSetCookie/ReqSetCookie": { "type": "Interface", "extends": [ { @@ -155,7 +135,7 @@ export const serviceProto: ServiceProto = { } ] }, - "PtlGetCookie/ResGetCookie": { + "PtlSetCookie/ResSetCookie": { "type": "Interface", "extends": [ { @@ -167,7 +147,7 @@ export const serviceProto: ServiceProto = { } ] }, - "PtlGetSession/ReqGetSession": { + "PtlSetSession/ReqSetSession": { "type": "Interface", "extends": [ { @@ -179,7 +159,7 @@ export const serviceProto: ServiceProto = { } ] }, - "PtlGetSession/ResGetSession": { + "PtlSetSession/ResSetSession": { "type": "Interface", "extends": [ { @@ -213,6 +193,16 @@ export const serviceProto: ServiceProto = { "target": "base/BaseResponse" } } + ], + "properties": [ + { + "id": 0, + "name": "testSession", + "type": { + "type": "String" + }, + "optional": true + } ] } }