Skip to content

Commit 221b135

Browse files
committed
feat: 优化配置归一化逻辑,使用可选链操作符简化代码
1 parent 888cfb0 commit 221b135

2 files changed

Lines changed: 35 additions & 32 deletions

File tree

packages/runtime/src/rest-server.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,47 @@ export class RestServer {
5151
* Normalize configuration with defaults
5252
*/
5353
private normalizeConfig(config: RestServerConfig): Required<RestServerConfig> {
54-
const api = config.api ?? {};
55-
const crud = config.crud ?? {};
56-
const metadata = config.metadata ?? {};
57-
const batch = config.batch ?? {};
58-
const routes = config.routes ?? {};
54+
const api = config.api;
55+
const crud = config.crud;
56+
const metadata = config.metadata;
57+
const batch = config.batch;
58+
const routes = config.routes;
5959

6060
return {
6161
api: {
62-
version: api.version ?? 'v1',
63-
basePath: api.basePath ?? '/api',
64-
apiPath: api.apiPath,
65-
enableCrud: api.enableCrud ?? true,
66-
enableMetadata: api.enableMetadata ?? true,
67-
enableBatch: api.enableBatch ?? true,
68-
enableDiscovery: api.enableDiscovery ?? true,
69-
documentation: api.documentation,
70-
responseFormat: api.responseFormat,
62+
version: api?.version ?? 'v1',
63+
basePath: api?.basePath ?? '/api',
64+
apiPath: api?.apiPath,
65+
enableCrud: api?.enableCrud ?? true,
66+
enableMetadata: api?.enableMetadata ?? true,
67+
enableBatch: api?.enableBatch ?? true,
68+
enableDiscovery: api?.enableDiscovery ?? true,
69+
documentation: api?.documentation,
70+
responseFormat: api?.responseFormat,
7171
},
7272
crud: {
73-
operations: crud.operations,
74-
patterns: crud.patterns,
75-
dataPrefix: crud.dataPrefix ?? '/data',
76-
objectParamStyle: crud.objectParamStyle ?? 'path',
73+
operations: crud?.operations,
74+
patterns: crud?.patterns,
75+
dataPrefix: crud?.dataPrefix ?? '/data',
76+
objectParamStyle: crud?.objectParamStyle ?? 'path',
7777
},
7878
metadata: {
79-
prefix: metadata.prefix ?? '/meta',
80-
enableCache: metadata.enableCache ?? true,
81-
cacheTtl: metadata.cacheTtl ?? 3600,
82-
endpoints: metadata.endpoints,
79+
prefix: metadata?.prefix ?? '/meta',
80+
enableCache: metadata?.enableCache ?? true,
81+
cacheTtl: metadata?.cacheTtl ?? 3600,
82+
endpoints: metadata?.endpoints,
8383
},
8484
batch: {
85-
maxBatchSize: batch.maxBatchSize ?? 200,
86-
enableBatchEndpoint: batch.enableBatchEndpoint ?? true,
87-
operations: batch.operations,
88-
defaultAtomic: batch.defaultAtomic ?? true,
85+
maxBatchSize: batch?.maxBatchSize ?? 200,
86+
enableBatchEndpoint: batch?.enableBatchEndpoint ?? true,
87+
operations: batch?.operations,
88+
defaultAtomic: batch?.defaultAtomic ?? true,
8989
},
9090
routes: {
91-
includeObjects: routes.includeObjects,
92-
excludeObjects: routes.excludeObjects,
93-
nameTransform: routes.nameTransform ?? 'none',
94-
overrides: routes.overrides,
91+
includeObjects: routes?.includeObjects,
92+
excludeObjects: routes?.excludeObjects,
93+
nameTransform: routes?.nameTransform ?? 'none',
94+
overrides: routes?.overrides,
9595
},
9696
};
9797
}

packages/runtime/src/route-manager.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { RouteHandler, IHttpServer } from '@objectstack/core';
2-
import { RouteHandlerMetadata, HttpMethod } from '@objectstack/spec';
2+
import { System, Shared } from '@objectstack/spec';
3+
4+
type RouteHandlerMetadata = System.RouteHandlerMetadata;
5+
type HttpMethod = Shared.HttpMethod;
36

47
/**
58
* Route Entry
69
* Internal representation of registered routes
710
*/
8-
interface RouteEntry {
11+
export interface RouteEntry {
912
method: HttpMethod;
1013
path: string;
1114
handler: RouteHandler;

0 commit comments

Comments
 (0)