session and cookie examples
This commit is contained in:
9
examples/session-and-cookie/backend/src/api/ApiClear.ts
Normal file
9
examples/session-and-cookie/backend/src/api/ApiClear.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqClear, ResClear } from "../shared/protocols/PtlClear";
|
||||
|
||||
export async function ApiClear(call: ApiCall<ReqClear, ResClear>) {
|
||||
call.succ({
|
||||
__cookie: {},
|
||||
__session: {}
|
||||
})
|
||||
}
|
13
examples/session-and-cookie/backend/src/api/ApiGetCookie.ts
Normal file
13
examples/session-and-cookie/backend/src/api/ApiGetCookie.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqGetCookie, ResGetCookie } from "../shared/protocols/PtlGetCookie";
|
||||
|
||||
let times = 0;
|
||||
|
||||
export async function ApiGetCookie(call: ApiCall<ReqGetCookie, ResGetCookie>) {
|
||||
call.succ({
|
||||
__cookie: {
|
||||
...call.req.__cookie,
|
||||
testCookie: 'Cookie ' + (++times),
|
||||
}
|
||||
})
|
||||
}
|
13
examples/session-and-cookie/backend/src/api/ApiGetSession.ts
Normal file
13
examples/session-and-cookie/backend/src/api/ApiGetSession.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqGetSession, ResGetSession } from "../shared/protocols/PtlGetSession";
|
||||
|
||||
let times = 0;
|
||||
|
||||
export async function ApiGetSession(call: ApiCall<ReqGetSession, ResGetSession>) {
|
||||
call.succ({
|
||||
__session: {
|
||||
...call.req.__session,
|
||||
testSession: 'Session ' + (++times)
|
||||
}
|
||||
})
|
||||
}
|
6
examples/session-and-cookie/backend/src/api/ApiTest.ts
Normal file
6
examples/session-and-cookie/backend/src/api/ApiTest.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ApiCall } from "tsrpc";
|
||||
import { ReqTest, ResTest } from "../shared/protocols/PtlTest";
|
||||
|
||||
export async function ApiTest(call: ApiCall<ReqTest, ResTest>) {
|
||||
call.succ({});
|
||||
}
|
38
examples/session-and-cookie/backend/src/index.ts
Normal file
38
examples/session-and-cookie/backend/src/index.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import * as path from "path";
|
||||
import { HttpServer } from "tsrpc";
|
||||
import { BaseRequest, BaseResponse } from "./shared/protocols/base";
|
||||
import { serviceProto } from "./shared/protocols/serviceProto";
|
||||
|
||||
// Create the Server
|
||||
const server = new HttpServer(serviceProto, {
|
||||
port: 3000,
|
||||
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;
|
||||
});
|
||||
|
||||
// Entry function
|
||||
async function main() {
|
||||
// Auto implement APIs
|
||||
await server.autoImplementApi(path.resolve(__dirname, 'api'));
|
||||
|
||||
// TODO
|
||||
// Prepare something... (e.g. connect the db)
|
||||
|
||||
await server.start();
|
||||
};
|
||||
|
||||
main().catch(e => {
|
||||
// Exit if any error during the startup
|
||||
server.logger.error(e);
|
||||
process.exit(-1);
|
||||
});
|
@@ -0,0 +1,9 @@
|
||||
import { BaseRequest, BaseResponse } from './base';
|
||||
|
||||
export interface ReqClear extends BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface ResClear extends BaseResponse {
|
||||
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { BaseRequest, BaseResponse } from './base'
|
||||
|
||||
export interface ReqGetCookie extends BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface ResGetCookie extends BaseResponse {
|
||||
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { BaseRequest, BaseResponse } from './base'
|
||||
|
||||
export interface ReqGetSession extends BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface ResGetSession extends BaseResponse {
|
||||
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
import { BaseRequest, BaseResponse } from './base'
|
||||
|
||||
export interface ReqTest extends BaseRequest {
|
||||
|
||||
}
|
||||
|
||||
export interface ResTest extends BaseResponse {
|
||||
|
||||
}
|
@@ -0,0 +1,9 @@
|
||||
export interface BaseRequest {
|
||||
__session?: { [key: string]: any };
|
||||
__cookie?: { [key: string]: any };
|
||||
}
|
||||
|
||||
export interface BaseResponse {
|
||||
__session?: { [key: string]: any };
|
||||
__cookie?: { [key: string]: any };
|
||||
}
|
@@ -0,0 +1,219 @@
|
||||
import { ServiceProto } from 'tsrpc-proto';
|
||||
import { ReqClear, ResClear } from './PtlClear';
|
||||
import { ReqGetCookie, ResGetCookie } from './PtlGetCookie';
|
||||
import { ReqGetSession, ResGetSession } from './PtlGetSession';
|
||||
import { ReqTest, ResTest } from './PtlTest';
|
||||
|
||||
export interface ServiceType {
|
||||
api: {
|
||||
"Clear": {
|
||||
req: ReqClear,
|
||||
res: ResClear
|
||||
},
|
||||
"GetCookie": {
|
||||
req: ReqGetCookie,
|
||||
res: ResGetCookie
|
||||
},
|
||||
"GetSession": {
|
||||
req: ReqGetSession,
|
||||
res: ResGetSession
|
||||
},
|
||||
"Test": {
|
||||
req: ReqTest,
|
||||
res: ResTest
|
||||
}
|
||||
},
|
||||
msg: {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export const serviceProto: ServiceProto<ServiceType> = {
|
||||
"version": 3,
|
||||
"services": [
|
||||
{
|
||||
"id": 3,
|
||||
"name": "Clear",
|
||||
"type": "api"
|
||||
},
|
||||
{
|
||||
"id": 0,
|
||||
"name": "GetCookie",
|
||||
"type": "api"
|
||||
},
|
||||
{
|
||||
"id": 1,
|
||||
"name": "GetSession",
|
||||
"type": "api"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Test",
|
||||
"type": "api"
|
||||
}
|
||||
],
|
||||
"types": {
|
||||
"PtlClear/ReqClear": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"base/BaseRequest": {
|
||||
"type": "Interface",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
"optional": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlClear/ResClear": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"base/BaseResponse": {
|
||||
"type": "Interface",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
},
|
||||
"optional": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlGetCookie/ReqGetCookie": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlGetCookie/ResGetCookie": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlGetSession/ReqGetSession": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlGetSession/ResGetSession": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlTest/ReqTest": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseRequest"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"PtlTest/ResTest": {
|
||||
"type": "Interface",
|
||||
"extends": [
|
||||
{
|
||||
"id": 0,
|
||||
"type": {
|
||||
"type": "Reference",
|
||||
"target": "base/BaseResponse"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user