Skip to content

Commit 913183a

Browse files
author
Joey Yang
committed
update demo configuration, fix template resolve bug
1 parent 648396b commit 913183a

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"kubectl",
1010
"protobuf",
1111
"quotemark",
12+
"stringify",
1213
"tmpl",
1314
"webhook",
1415
"woff"

conf/_demo/routes/get-user.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ relay:
1414
original-accept-encoding: ${this.accept}
1515
token-from-context: ${this.token}
1616
body: |-
17-
{ id: "${console.log(ctx.request) && ctx.params}", data: "demo" }
17+
{ id: "${ctx.params.userId}", data: "demo" }
1818
interceptors:
1919
relay-interceptor: functions/relay-interceptor.ts
2020
response:
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
id: "${JSON.stringify(ctx.relayResultMap.get('demoPost')})"
2+
relayResult: ${JSON.stringify(ctx.relayResultMap.get('demoPost').data, null, 2)},
3+
relayStatusCode: "${ctx.relayResultMap.get('demoPost').status}"
34
}

src/storage/conf-normalizer.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,52 +78,62 @@ export class ConfNormalizer {
7878
}
7979

8080
public static async loadRelatedTmplAndFuncFiles(confObj: ApplicationConfig) {
81-
// todo load extra template files
82-
await this.loadCodeInConf(confObj.initContext.initFunctions, confObj);
83-
await this.loadCodeInConf(confObj.initContext.functions, confObj);
81+
await this.loadCodeOrTmplInConf(confObj.initContext.initFunctions, confObj);
82+
await this.loadCodeOrTmplInConf(confObj.initContext.functions, confObj);
8483

8584
for (let route of confObj.routes) {
8685
for (let handler of route.extract.headerHandlers) {
8786
if (handler.validate) {
88-
handler.validate = await this.loadCodeInConf(handler.validate, confObj) as string;
87+
handler.validate = await this.loadCodeOrTmplInConf(handler.validate, confObj) as string;
8988
}
9089
}
9190
for (let handler of route.extract.bodyHandlers) {
9291
if (handler.validate) {
93-
handler.validate = await this.loadCodeInConf(handler.validate, confObj) as string;
92+
handler.validate = await this.loadCodeOrTmplInConf(handler.validate, confObj) as string;
9493
}
9594
}
9695
for (let relayReq of route.relay) {
96+
relayReq.body = await this.loadCodeOrTmplInConf(relayReq.body || '', confObj, true) as string;
97+
if (!relayReq.headers) {
98+
relayReq.headers = {};
99+
}
100+
await this.loadCodeOrTmplInConf(relayReq.headers, confObj, true);
101+
relayReq.location = await this.loadCodeOrTmplInConf(relayReq.location, confObj, true) as string;
97102
if (relayReq.interceptors) {
98-
await this.loadCodeInConf(relayReq.interceptors, confObj);
103+
await this.loadCodeOrTmplInConf(relayReq.interceptors, confObj);
99104
}
100105
}
106+
if (!route.response.headers) {
107+
route.response.headers = {};
108+
}
109+
await this.loadCodeOrTmplInConf(route.response.headers, confObj, true);
110+
route.response.body = await this.loadCodeOrTmplInConf(route.response.body || '', confObj, true) as string;
101111
if (route.response.interceptors) {
102-
await this.loadCodeInConf(route.response.interceptors, confObj);
112+
await this.loadCodeOrTmplInConf(route.response.interceptors, confObj);
103113
}
104114
}
105115
return confObj;
106116
}
107117

108-
private static async loadCodeInConf(target: KVPair | string, confObj: ApplicationConfig) {
118+
private static async loadCodeOrTmplInConf(target: KVPair | string, confObj: ApplicationConfig, isTemplate = false) {
109119
if (typeof target === 'string') {
110-
return await this.loadSingleFuncOrTmplCode(target, confObj);
120+
return await this.loadSingleFuncOrTmplCode(target, confObj, isTemplate);
111121
} else {
112122
for (let funcName in target) {
113123
let funcStr = target[funcName].trim();
114-
target[funcName] = await this.loadSingleFuncOrTmplCode(funcStr, confObj);
124+
target[funcName] = await this.loadSingleFuncOrTmplCode(funcStr, confObj, isTemplate);
115125
}
116126
}
117127
return target;
118128
}
119129

120-
private static async loadSingleFuncOrTmplCode(target: string, confObj: ApplicationConfig) {
130+
private static async loadSingleFuncOrTmplCode(target: string, confObj: ApplicationConfig, isTemplate: boolean) {
121131
let content = target;
122132
// load content data from files
123133
if (target && (target.endsWith('.js') || target.endsWith('.ts') || target.endsWith('.tmpl'))) {
124134
content = await ConfigManager.storage.loadSeparateContent(target, confObj);
125135
}
126-
if (target.endsWith('.tmpl')) {
136+
if (isTemplate) {
127137
return content;
128138
}
129139
// parse TS/JS function using TS Compiler

src/storage/storage.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ export abstract class ConfStorage {
2929

3030
abstract initialize(): Promise<void>;
3131

32-
// some storage types such as db need disconnect manually, make sure no resource leak
33-
abstract dispose(): Promise<void>;
34-
3532
abstract listAllConfigurations(): Promise<ApplicationConfig[]>;
3633

3734
abstract findConfigurationByName(appName: string): Promise<ApplicationConfig>;
@@ -44,6 +41,8 @@ export abstract class ConfStorage {
4441

4542
abstract loadSeparateContent(targetFile: string, confObj: ApplicationConfig): Promise<string>;
4643

44+
// some storage types such as db need disconnect manually, make sure no resource leak
45+
abstract dispose(): Promise<void>;
4746
// abstract exportConf();
4847

4948
// abstract importConf();

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"resolveJsonModule": true,
44
"module": "commonjs",
55
"esModuleInterop": true,
6+
"declaration": true,
67
"target": "ES2017",
78
"noImplicitAny": true,
89
"moduleResolution": "node",

0 commit comments

Comments
 (0)