Skip to content

Commit 55950b9

Browse files
Update types
1 parent 82ea1f3 commit 55950b9

File tree

10 files changed

+77
-59
lines changed

10 files changed

+77
-59
lines changed

src/registry/domain/components-cache/components-list.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export default function componentsList(conf: Config, cdn: Cdn) {
88
`${conf.storage.options.componentsDir}/components.json`;
99

1010
const componentsList = {
11-
getFromJson: (callback: Callback<any>) =>
11+
getFromJson: (callback: Callback<any, string>) =>
1212
cdn.getJson(filePath(), true, callback),
1313

1414
getFromDirectories: (callback: Callback<ComponentsList, string>) => {
15-
const componentsInfo = {};
15+
const componentsInfo: Dictionary<string[]> = {};
1616

1717
const getVersionsForComponent = (
1818
componentName: string,
@@ -49,11 +49,11 @@ export default function componentsList(conf: Config, cdn: Cdn) {
4949
getVersionsForComponent,
5050
(errors, versions) => {
5151
if (errors) {
52-
return callback(errors, undefined as any);
52+
return callback(errors as any, undefined as any);
5353
}
5454

5555
components.forEach((component, i) => {
56-
componentsInfo[component] = versions[i];
56+
componentsInfo[component] = (versions as any)[i];
5757
});
5858

5959
callback(null, {

src/registry/domain/components-cache/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import getUnixUTCTimestamp from 'oc-get-unix-utc-timestamp';
55
import { Cdn, ComponentsList, Config } from '../../../types';
66

77
export default function componentsCache(conf: Config, cdn: Cdn) {
8-
let cachedComponentsList: ComponentsList, refreshLoop;
8+
let cachedComponentsList: ComponentsList;
9+
let refreshLoop: NodeJS.Timeout;
910

1011
const componentsList = getComponentsList(conf, cdn);
1112

@@ -37,9 +38,13 @@ export default function componentsCache(conf: Config, cdn: Cdn) {
3738
callback(null, data);
3839
};
3940

40-
const returnError = (code, message, callback) => {
41+
const returnError = (
42+
code: string,
43+
message: string,
44+
callback: Callback<any, any>
45+
) => {
4146
eventsHandler.fire('error', { code, message });
42-
return callback(code);
47+
return callback(code, undefined as any);
4348
};
4449

4550
return {
@@ -55,7 +60,7 @@ export default function componentsCache(conf: Config, cdn: Cdn) {
5560
callback(null, cachedComponentsList);
5661
},
5762

58-
load(callback) {
63+
load(callback: Callback<ComponentsList>) {
5964
componentsList.getFromJson((jsonErr, jsonComponents) => {
6065
componentsList.getFromDirectories((dirErr, dirComponents) => {
6166
if (dirErr) {
@@ -76,7 +81,7 @@ export default function componentsCache(conf: Config, cdn: Cdn) {
7681
});
7782
});
7883
},
79-
refresh(callback) {
84+
refresh(callback: Callback<ComponentsList>) {
8085
clearTimeout(refreshLoop);
8186
componentsList.refresh((err, components) => {
8287
if (err) {

src/registry/domain/components-details.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
export default function componentsDetails(conf: Config, cdn: Cdn) {
1414
const returnError = (
1515
code: string,
16-
message: string,
16+
message: string | Error,
1717
callback: (code: string) => void
1818
) => {
1919
eventsHandler.fire('error', { code, message });
@@ -23,12 +23,12 @@ export default function componentsDetails(conf: Config, cdn: Cdn) {
2323
const filePath = (): string =>
2424
`${conf.storage.options.componentsDir}/components-details.json`;
2525

26-
const getFromJson = (callback: Callback<ComponentsDetails>) =>
26+
const getFromJson = (callback: Callback<ComponentsDetails, string>) =>
2727
cdn.getJson<ComponentsDetails>(filePath(), true, callback);
2828

2929
const getFromDirectories = (
3030
options: { componentsList: ComponentsList; details: ComponentsDetails },
31-
callback
31+
callback: Callback<ComponentsDetails, Error | undefined>
3232
) => {
3333
const details = _.extend({}, _.cloneDeep(options.details));
3434
details.components = details.components || {};
@@ -52,7 +52,7 @@ export default function componentsDetails(conf: Config, cdn: Cdn) {
5252
true,
5353
(err, content) => {
5454
if (err) {
55-
return next(err);
55+
return next(err as any);
5656
}
5757
details.components[name][version] = {
5858
publishDate: content.oc.date || 0

src/registry/domain/extract-package.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ export default function extractPackage(
1313
outputFolder: string;
1414
packageJson: any;
1515
}>
16-
) {
17-
const packageFile = files[0],
18-
packagePath = path.resolve(packageFile.path),
19-
packageUntarOutput = path.resolve(
20-
packageFile.path,
21-
'..',
22-
packageFile.filename.replace('.tar.gz', '')
23-
),
24-
packageOutput = path.resolve(packageUntarOutput, '_package');
16+
): void {
17+
const packageFile: Express.Multer.File = (files as any)[0];
18+
const packagePath = path.resolve(packageFile.path);
19+
const packageUntarOutput = path.resolve(
20+
packageFile.path,
21+
'..',
22+
packageFile.filename.replace('.tar.gz', '')
23+
);
24+
const packageOutput = path.resolve(packageUntarOutput, '_package');
2525

2626
targz.decompress(
2727
{

src/registry/domain/options-sanitiser.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface Input extends Partial<Omit<Config, 'beforePublish'>> {
99
baseUrl: string;
1010
}
1111

12-
export default function optionsSanitiser(input: Input) {
12+
export default function optionsSanitiser(input: Input): Config {
1313
const options = _.clone(input);
1414

1515
if (!options.publishAuth) {
@@ -107,5 +107,5 @@ export default function optionsSanitiser(input: Input) {
107107
options.env = {};
108108
}
109109

110-
return options;
110+
return options as Config;
111111
}

src/registry/domain/plugins-initialiser.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ function validatePlugins(plugins: unknown[]): asserts plugins is Plugin[] {
1717
!_.isString((plugin as Plugin).name)
1818
) {
1919
throw new Error(
20-
strings.errors.registry.PLUGIN_NOT_VALID((plugin as Plugin).name || c)
20+
strings.errors.registry.PLUGIN_NOT_VALID(
21+
(plugin as Plugin).name || String(c)
22+
)
2123
);
2224
}
2325
});
@@ -51,14 +53,17 @@ const defer = function(plugin: Plugin, cb: (err?: Error) => void) {
5153
return cb();
5254
};
5355

54-
export function init(pluginsToRegister: unknown[], callback) {
55-
const registered = {};
56+
export function init(
57+
pluginsToRegister: unknown[],
58+
callback: Callback<Dictionary<(...args: unknown[]) => void>, unknown>
59+
): void {
60+
const registered: Dictionary<(...args: unknown[]) => void> = {};
5661

5762
try {
5863
validatePlugins(pluginsToRegister);
5964
checkDependencies(pluginsToRegister);
6065
} catch (err) {
61-
return callback(err);
66+
return callback(err, undefined as any);
6267
}
6368

6469
const dependenciesRegistered = (dependencies: string[]) => {
@@ -93,27 +98,31 @@ export function init(pluginsToRegister: unknown[], callback) {
9398

9499
const dependencies = _.pick(registered, plugin.register.dependencies);
95100

96-
plugin.register.register(plugin.options || {}, dependencies, err => {
97-
const pluginCallback = plugin.callback || _.noop;
98-
pluginCallback(err);
99-
// Overriding toString so implementation details of plugins do not
100-
// leak to OC consumers
101-
plugin.register.execute.toString = () => plugin.description || '';
102-
registered[plugin.name] = plugin.register.execute;
103-
done(err);
104-
});
101+
plugin.register.register(
102+
plugin.options || {},
103+
dependencies,
104+
(err?: Error) => {
105+
const pluginCallback = plugin.callback || _.noop;
106+
pluginCallback(err);
107+
// Overriding toString so implementation details of plugins do not
108+
// leak to OC consumers
109+
plugin.register.execute.toString = () => plugin.description || '';
110+
registered[plugin.name] = plugin.register.execute;
111+
done(err);
112+
}
113+
);
105114
};
106115

107116
const terminator = function(err: Error) {
108117
if (deferredLoads.length > 0) {
109118
const deferredPlugins = _.clone(deferredLoads);
110119
deferredLoads = [];
111120

112-
return async.mapSeries(deferredPlugins, loadPlugin, terminator);
121+
return async.mapSeries(deferredPlugins, loadPlugin, terminator as any);
113122
}
114123

115124
callback(err, registered);
116125
};
117126

118-
async.mapSeries(pluginsToRegister, loadPlugin, terminator);
127+
async.mapSeries(pluginsToRegister, loadPlugin, terminator as any);
119128
}

src/registry/domain/register-templates.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,24 @@ import _ from 'lodash';
66

77
export default function registerTemplates(
88
extraTemplates: Template[]
9-
): { templatesHash: Dictionary<Template>; templatesInfo: TemplateInfo[] } {
9+
): {
10+
templatesHash: Dictionary<Template>;
11+
templatesInfo: TemplateInfo[];
12+
} {
1013
const coreTemplates: Template[] = [
1114
es6Template,
1215
jadeTemplate,
1316
handlebarsTemplate
1417
];
1518
const templates = _.union(coreTemplates, extraTemplates);
1619
const templatesHash = templates.reduce((hash, template) => {
17-
try {
18-
const type = template.getInfo().type;
19-
hash[type] = template;
20-
return hash;
21-
} catch (err) {
22-
throw err;
23-
}
20+
const type = template.getInfo().type;
21+
hash[type] = template;
22+
return hash;
2423
}, {} as Dictionary<Template>);
2524

2625
const templatesInfo = templates.map(template => {
27-
try {
28-
return template.getInfo();
29-
} catch (err) {
30-
throw err;
31-
}
26+
return template.getInfo();
3227
});
3328

3429
return {

src/registry/domain/sanitiser.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ export function sanitiseComponentParameters(
3737
requestParameters: Dictionary<string | number | boolean>,
3838
expectedParameters: Dictionary<OcParameter>
3939
): Dictionary<string | number | boolean> {
40-
const result = {};
40+
const result: Dictionary<string | number | boolean> = {};
4141

4242
for (const [requestParameterName, requestParameter] of Object.entries(
4343
requestParameters
4444
)) {
4545
if (
4646
typeof expectedParameters === 'object' &&
47+
// eslint-disable-next-line no-prototype-builtins
4748
expectedParameters.hasOwnProperty(requestParameterName)
4849
) {
4950
const expectedType = expectedParameters[requestParameterName].type;

src/registry/domain/version-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import semverExtra from 'semver-extra';
44
export function getAvailableVersion(
55
requestedVersion: string | undefined,
66
availableVersions: string[]
7-
) {
7+
): string | undefined {
88
if (typeof requestedVersion === 'undefined') {
99
requestedVersion = '';
1010
}
@@ -20,6 +20,6 @@ export function getAvailableVersion(
2020
export function validateNewVersion(
2121
requestedVersion: string,
2222
availableVersions: string[]
23-
) {
23+
): boolean {
2424
return !availableVersions.includes(requestedVersion);
2525
}

src/types.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ export interface Config {
168168
}
169169

170170
export interface Cdn {
171-
getJson: <T>(filePath: string, force: boolean, cb: Callback<T>) => void;
171+
getJson: <T>(
172+
filePath: string,
173+
force: boolean,
174+
cb: Callback<T, string>
175+
) => void;
172176
listSubDirectories: (
173177
dir: string,
174178
cb: Callback<string[], Error & { code?: string }>
@@ -192,13 +196,17 @@ export interface Template {
192196
export interface Plugin {
193197
name: string;
194198
register: {
195-
register: Function;
196-
execute: Function;
199+
register: (
200+
options: unknown,
201+
dependencies: unknown,
202+
next: () => void
203+
) => void;
204+
execute: (...args: unknown[]) => unknown;
197205
dependencies: string[];
198206
};
199207
description?: string;
200208
options?: any;
201-
callback: Function;
209+
callback?: (...args: unknown[]) => void;
202210
}
203211

204212
declare global {

0 commit comments

Comments
 (0)