Skip to content

Commit f63ffe3

Browse files
Merge pull request #1248 from opencomponents/improve-type-url-funcs
Type better baseUrlFunc and discoveryFunc
2 parents ec5c420 + b76559c commit f63ffe3

File tree

4 files changed

+54
-58
lines changed

4 files changed

+54
-58
lines changed

src/registry/middleware/base-url-handler.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ export default function baseUrlHandler(
55
res: Response,
66
next: NextFunction
77
): void {
8-
res.conf.baseUrlFunc =
9-
res.conf.baseUrlFunc ||
10-
(typeof res.conf.baseUrl === 'function' ? res.conf.baseUrl : undefined);
11-
128
if (res.conf.baseUrlFunc) {
139
res.conf.baseUrl = res.conf.baseUrlFunc({
1410
host: req.headers.host,

src/registry/routes/dependencies.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import getAvailableDependencies from './helpers/get-available-dependencies';
44

55
export default function dependencies(conf: Config) {
66
return function (req: Request, res: Response): void {
7-
if (conf.discovery) {
7+
if (res.conf.discovery) {
88
const dependencies = getAvailableDependencies(conf.dependencies).map(
99
({ core, name, version }) => {
1010
const dep: { name: string; core: boolean; versions?: string[] } = {

src/registry/routes/plugins.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Config } from '../../types';
33

44
export default function plugins(conf: Config) {
55
return (req: Request, res: Response): void => {
6-
if (conf.discovery) {
6+
if (res.conf.discovery) {
77
const plugins = Object.entries(conf.plugins).map(
88
([pluginName, pluginFn]) => ({
99
name: pluginName,

src/types.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { Logger } from './cli/logger';
33
import { PackageJson } from 'type-fest';
44

55
export interface Author {
6-
name?: string;
76
email?: string;
7+
name?: string;
88
url?: string;
99
}
1010

@@ -21,40 +21,40 @@ interface ComponentHistory {
2121
}
2222

2323
export interface TemplateInfo {
24-
type: string;
25-
version: string;
2624
externals: Array<{
2725
name: string;
2826
global: string | string[];
2927
url: string;
3028
}>;
29+
type: string;
30+
version: string;
3131
}
3232

3333
export interface ComponentsDetails {
34-
lastEdit: number;
3534
components: {
3635
[componentName: string]: {
3736
[componentVersion: string]: { publishDate: number };
3837
};
3938
};
39+
lastEdit: number;
4040
}
4141

4242
export interface ComponentsList {
43-
lastEdit: number;
4443
components: Dictionary<string[]>;
44+
lastEdit: number;
4545
}
4646

4747
export interface OcParameter {
48+
default?: string | boolean | number;
4849
description?: string;
4950
example?: string;
5051
mandatory?: boolean;
5152
type: 'string' | 'boolean' | 'number';
52-
default?: string | boolean | number;
5353
}
5454

5555
interface OcConfiguration {
56+
container?: boolean;
5657
date: number;
57-
state?: 'deprecated' | 'experimental';
5858
files: {
5959
dataProvider: {
6060
hashKey: string;
@@ -71,32 +71,32 @@ interface OcConfiguration {
7171
};
7272
packaged: boolean;
7373
parameters: Record<string, OcParameter>;
74-
stringifiedDate: string;
75-
version: string;
7674
plugins?: string[];
77-
container?: boolean;
7875
renderInfo?: boolean;
76+
state?: 'deprecated' | 'experimental';
77+
stringifiedDate: string;
78+
version: string;
7979
}
8080

8181
export interface Component extends PackageJson {
82-
name: string;
83-
version: string;
8482
allVersions: string[];
83+
name: string;
8584
oc: OcConfiguration;
85+
version: string;
8686
}
8787

8888
export interface ParsedComponent extends Omit<Component, 'author'> {
8989
author: Author;
9090
}
9191

9292
export interface VM {
93-
availablePlugins: Record<string, (...args: unknown[]) => void>;
9493
availableDependencies: Array<{
9594
core: boolean;
9695
name: string;
9796
version: string;
9897
link: string;
9998
}>;
99+
availablePlugins: Record<string, (...args: unknown[]) => void>;
100100
components: ParsedComponent[];
101101
componentsHistory?: ComponentHistory[];
102102
componentsList: ComponentList[];
@@ -114,77 +114,77 @@ export interface VM {
114114
}
115115

116116
export interface Config {
117-
beforePublish: (req: Request, res: Response, next: NextFunction) => void;
118117
baseUrl: string;
119-
baseUrlFunc: (opts: { host?: string; secure: boolean }) => string;
118+
baseUrlFunc?: (opts: { host?: string; secure: boolean }) => string;
119+
beforePublish: (req: Request, res: Response, next: NextFunction) => void;
120+
customHeadersToSkipOnWeakVersion: string[];
121+
dependencies: string[];
120122
discovery: boolean;
121-
discoveryFunc: (opts: { host?: string; secure: boolean }) => boolean;
122-
plugins: Record<string, (...args: unknown[]) => void>;
123+
discoveryFunc?: (opts: { host?: string; secure: boolean }) => boolean;
124+
env: Dictionary<string>;
125+
executionTimeout?: number;
126+
fallbackRegistryUrl: string;
127+
hotReloading: boolean;
128+
keepAliveTimeout?: number;
129+
liveReloadPort: number;
123130
local: boolean;
124-
tempDir: string;
131+
path: string;
132+
plugins: Record<string, (...args: unknown[]) => void>;
133+
pollingInterval: number;
125134
port: number;
126135
postRequestPayloadSize?: number;
127-
verbosity: number;
128136
prefix: string;
129-
path: string;
130137
publishAuth?: {
131138
type: string;
132139
username: string;
133140
password: string;
134141
};
135-
dependencies: string[];
142+
publishValidation: (data: unknown) =>
143+
| {
144+
isValid: boolean;
145+
error?: string;
146+
}
147+
| boolean;
148+
refreshInterval?: number;
136149
routes?: Array<{
137150
route: string;
138151
method: string;
139152
handler: (req: Request, res: Response) => void;
140153
}>;
141-
storage: {
142-
adapter: any;
143-
options: Dictionary<any> & { componentsDir: string };
144-
};
145154
s3?: {
146155
bucket: string;
147156
region: string;
148157
key?: string;
149158
secret?: string;
150159
componentsDir: string;
151160
};
152-
customHeadersToSkipOnWeakVersion: string[];
153-
fallbackRegistryUrl: string;
154-
pollingInterval: number;
155-
publishValidation: (data: unknown) =>
156-
| {
157-
isValid: boolean;
158-
error?: string;
159-
}
160-
| boolean;
161-
refreshInterval?: number;
162-
keepAliveTimeout?: number;
161+
storage: {
162+
adapter: any;
163+
options: Dictionary<any> & { componentsDir: string };
164+
};
165+
tempDir: string;
163166
templates: any[];
164-
env: Dictionary<string>;
165-
hotReloading: boolean;
166167
timeout: number;
167-
liveReloadPort: number;
168-
executionTimeout?: number;
168+
verbosity: number;
169169
}
170170

171171
export interface Cdn {
172+
adapterType: string;
173+
getFile: (filePath: string, cb: Callback<string>) => void;
172174
getJson<T>(filePath: string, force: boolean, cb: Callback<T, string>): void;
173175
getJson<T>(filePath: string, cb: Callback<T, string>): void;
174-
getFile: (filePath: string, cb: Callback<string>) => void;
175-
putDir: (folderPath: string, filePath: string, cb: Callback) => void;
176176
listSubDirectories: (
177177
dir: string,
178178
cb: Callback<string[], Error & { code?: string }>
179179
) => void;
180+
maxConcurrentRequests: number;
181+
putDir: (folderPath: string, filePath: string, cb: Callback) => void;
180182
putFileContent: (
181183
data: unknown,
182184
path: string,
183185
isPrivate: boolean,
184186
callback: Callback<unknown, string>
185187
) => void;
186-
maxConcurrentRequests: number;
187-
adapterType: string;
188188
}
189189

190190
type CompiledTemplate = (model: unknown) => string;
@@ -203,21 +203,24 @@ interface CompilerOptions {
203203
}
204204

205205
export interface Template {
206-
getInfo: () => TemplateInfo;
206+
compile?: (options: CompilerOptions, cb: Callback) => void;
207207
getCompiledTemplate: (
208208
templateString: string,
209209
key: string,
210210
context: Record<string, unknown>
211211
) => CompiledTemplate;
212+
getInfo: () => TemplateInfo;
212213
render: (
213214
options: { model: unknown; template: CompiledTemplate },
214215
cb: Callback<string>
215216
) => void;
216-
compile?: (options: CompilerOptions, cb: Callback) => void;
217217
}
218218

219219
export interface Plugin {
220+
callback?: (...args: unknown[]) => void;
221+
description?: string;
220222
name: string;
223+
options?: any;
221224
register: {
222225
register: (
223226
options: unknown,
@@ -227,9 +230,6 @@ export interface Plugin {
227230
execute: (...args: unknown[]) => unknown;
228231
dependencies?: string[];
229232
};
230-
description?: string;
231-
options?: any;
232-
callback?: (...args: unknown[]) => void;
233233
}
234234

235235
export interface RegistryCli {
@@ -332,15 +332,15 @@ export interface Repository {
332332
filePath: string;
333333
}>
334334
): void;
335-
getStaticClientPath: () => string;
336335
getStaticClientMapPath: () => string;
336+
getStaticClientPath: () => string;
337337
getStaticFilePath: (
338338
componentName: string,
339339
componentVersion: string,
340340
filePath: string
341341
) => string;
342-
getTemplatesInfo: () => TemplateInfo[];
343342
getTemplate: (type: string) => Template;
343+
getTemplatesInfo: () => TemplateInfo[];
344344
init(callback: Callback<ComponentsList | string>): void;
345345
publishComponent(
346346
pkgDetails: any,
@@ -355,8 +355,8 @@ declare global {
355355
namespace Express {
356356
interface Response {
357357
conf: Config;
358-
errorDetails?: string;
359358
errorCode?: string;
359+
errorDetails?: string;
360360
}
361361
}
362362
}

0 commit comments

Comments
 (0)