Skip to content

Commit 9af5c92

Browse files
Merge pull request #321 from opencomponents/typescript
Typescript
2 parents 98f5f75 + 634b051 commit 9af5c92

38 files changed

+523
-315
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ npm-debug.log
2222
node_modules
2323
coverage
2424
/mocks/handlebars-component/_compile-static-package6/
25-
package-lock.json
25+
package-lock.json
26+
lib/

globals.d.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
declare module 'universalify' {
2+
export function fromCallback<
3+
Arguments extends readonly unknown[],
4+
ErrorValue,
5+
ReturnValue
6+
>(
7+
fn: (
8+
...arguments_: [
9+
...Arguments,
10+
(error: ErrorValue, value: ReturnValue) => void
11+
]
12+
) => void
13+
): {
14+
(...arguments_: Arguments): Promise<ReturnValue>;
15+
(
16+
...arguments_: [
17+
...Arguments,
18+
(error: ErrorValue, value: ReturnValue) => void
19+
]
20+
): void;
21+
};
22+
23+
export function fromPromise<
24+
Arguments extends readonly unknown[],
25+
ReturnValue
26+
>(
27+
fn: (...arguments_: [...Arguments]) => Promise<ReturnValue>
28+
): {
29+
(...arguments_: Arguments): Promise<ReturnValue>;
30+
(
31+
...arguments_: [
32+
...Arguments,
33+
(error: unknown, value: ReturnValue) => void
34+
]
35+
): void;
36+
};
37+
}
38+
39+
declare module 'nice-cache' {
40+
class Cache {
41+
constructor(opt: { refreshInterval?: number; verbose?: boolean });
42+
43+
get(type: string, key: string): any;
44+
set(type: string, key: string, data: unknown): void;
45+
sub(
46+
type: string,
47+
key: string,
48+
subscriber: (...args: unknown[]) => void
49+
): void;
50+
}
51+
52+
export = Cache;
53+
}
54+
55+
declare module 'stringformat' {
56+
function format(...args: string[]): string;
57+
58+
export = format;
59+
}

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"clean": "lerna clean",
77
"dev": "jest --watch",
88
"updated": "lerna updated",
9-
"test": "jest",
10-
"publish": "lerna publish --exact",
9+
"test": "lerna run tsc && jest",
10+
"publish": "lerna run tsc && lerna publish --exact",
1111
"precommit": "lint-staged",
1212
"prepare": "husky install"
1313
},
@@ -23,6 +23,7 @@
2323
"collectCoverage": true
2424
},
2525
"devDependencies": {
26+
"@types/node": "18.8.3",
2627
"@typescript-eslint/eslint-plugin": "5.0.0",
2728
"@typescript-eslint/parser": "5.0.0",
2829
"eslint": "8.0.1",
@@ -33,6 +34,6 @@
3334
"lerna": "4.0.0",
3435
"lint-staged": "10.0.7",
3536
"prettier": "2.4.1",
36-
"typescript": "4.4.4"
37+
"typescript": "4.8.4"
3738
}
38-
}
39+
}

packages/oc-azure-storage-adapter/__test__/azure.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const Readable = require('stream').Readable;
2-
const azure = require('../');
2+
const azure = require('../lib');
33

44
//Mock Date functions
55
const DATE_TO_USE = new Date('2017');

packages/oc-azure-storage-adapter/__test__/privateFilesExclusion.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const adapter = require('../');
1+
const adapter = require('../lib');
22

33
jest.mock('async', () => {
44
return {

packages/oc-azure-storage-adapter/index.d.ts

Lines changed: 0 additions & 24 deletions
This file was deleted.

packages/oc-azure-storage-adapter/package.json

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
"name": "oc-azure-storage-adapter",
33
"version": "0.2.0",
44
"description": "Azure storage adapter for OC",
5-
"main": "index.js",
6-
"types": "index.d.ts",
5+
"main": "lib/index.js",
6+
"types": "lib/index.d.ts",
7+
"scripts": {
8+
"tsc": "tsc",
9+
"test": "echo \"Error: run tests from root\" && exit 1"
10+
},
711
"files": [
8-
"index.js",
9-
"index.d.ts",
10-
"LICENSE",
11-
"README.md"
12+
"lib"
1213
],
1314
"repository": {
1415
"type": "git",
@@ -39,5 +40,11 @@
3940
"oc-storage-adapters-utils": "1.1.0",
4041
"stringformat": "^0.0.5",
4142
"universalify": "^2.0.0"
43+
},
44+
"devDependencies": {
45+
"@types/async": "3.2.15",
46+
"@types/fs-extra": "9.0.13",
47+
"@types/lodash": "4.14.186",
48+
"@types/node-dir": "0.0.34"
4249
}
43-
}
50+
}

packages/oc-azure-storage-adapter/index.js renamed to packages/oc-azure-storage-adapter/src/index.ts

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1-
'use strict';
2-
3-
const async = require('async');
4-
const azure = require('azure-storage');
5-
const Cache = require('nice-cache');
6-
const format = require('stringformat');
7-
const fs = require('fs-extra');
8-
const nodeDir = require('node-dir');
9-
const _ = require('lodash');
10-
const stream = require('stream');
11-
const { fromCallback } = require('universalify');
12-
13-
const { getFileInfo, strings } = require('oc-storage-adapters-utils');
14-
15-
module.exports = function (conf) {
1+
import async from 'async';
2+
import azure from 'azure-storage';
3+
import Cache from 'nice-cache';
4+
import format from 'stringformat';
5+
import fs from 'fs-extra';
6+
import nodeDir from 'node-dir';
7+
import _ from 'lodash';
8+
import stream from 'stream';
9+
import { fromCallback } from 'universalify';
10+
11+
import {
12+
getFileInfo,
13+
strings,
14+
StorageAdapter
15+
} from 'oc-storage-adapters-utils';
16+
17+
export interface AzureConfig {
18+
publicContainerName: string;
19+
privateContainerName: string;
20+
accountName: string;
21+
accountKey: string;
22+
path: string;
23+
verbosity?: boolean;
24+
refreshInterval?: number;
25+
}
26+
27+
export default function azureAdapter(conf: AzureConfig): StorageAdapter {
1628
const isValid = () => {
1729
if (
1830
!conf.publicContainerName ||
@@ -33,19 +45,19 @@ module.exports = function (conf) {
3345
const getClient = () =>
3446
azure.createBlobService(conf.accountName, conf.accountKey);
3547

36-
const getFile = (filePath, force, callback) => {
48+
const getFile = (filePath: string, force: boolean, callback: any) => {
3749
if (_.isFunction(force)) {
3850
callback = force;
3951
force = false;
4052
}
4153

42-
const getFromAzure = cb => {
54+
const getFromAzure = (cb: any) => {
4355
getClient().getBlobToText(
4456
conf.privateContainerName,
4557
filePath,
4658
(err, fileContent) => {
4759
if (err) {
48-
if (err.statusCode === 404) {
60+
if ((err as any).statusCode === 404) {
4961
return cb({
5062
code: strings.errors.STORAGE.FILE_NOT_FOUND_CODE,
5163
msg: format(strings.errors.STORAGE.FILE_NOT_FOUND, filePath)
@@ -70,7 +82,7 @@ module.exports = function (conf) {
7082
return callback(null, cached);
7183
}
7284

73-
getFromAzure((err, result) => {
85+
getFromAzure((err: Error | null, result: any) => {
7486
if (err) {
7587
return callback(err);
7688
}
@@ -80,13 +92,13 @@ module.exports = function (conf) {
8092
});
8193
};
8294

83-
const getJson = (filePath, force, callback) => {
95+
const getJson = (filePath: string, force: boolean, callback: any) => {
8496
if (_.isFunction(force)) {
8597
callback = force;
8698
force = false;
8799
}
88100

89-
getFile(filePath, force, (err, file) => {
101+
getFile(filePath, force, (err: Error | null, file: string) => {
90102
if (err) {
91103
return callback(err);
92104
}
@@ -104,21 +116,21 @@ module.exports = function (conf) {
104116
});
105117
};
106118

107-
const getUrl = (componentName, version, fileName) =>
119+
const getUrl = (componentName: string, version: string, fileName: string) =>
108120
`${conf.path}${componentName}/${version}/${fileName}`;
109121

110-
const listSubDirectories = (dir, callback) => {
122+
const listSubDirectories = (dir: string, callback: any) => {
111123
const normalisedPath =
112124
dir.lastIndexOf('/') === dir.length - 1 && dir.length > 0
113125
? dir
114126
: `${dir}/`;
115127

116128
const listBlobsWithPrefix = (
117-
azureClient,
118-
containerName,
119-
prefix,
120-
continuationToken,
121-
callback
129+
azureClient: azure.BlobService,
130+
containerName: string,
131+
prefix: string,
132+
continuationToken: azure.common.ContinuationToken,
133+
callback: any
122134
) => {
123135
azureClient.listBlobsSegmentedWithPrefix(
124136
containerName,
@@ -165,7 +177,7 @@ module.exports = function (conf) {
165177
containerName,
166178
prefix,
167179
result.continuationToken,
168-
(err, entryNames) => {
180+
(err: Error | null, entryNames: string[]) => {
169181
if (err) {
170182
return callback(err);
171183
}
@@ -182,15 +194,15 @@ module.exports = function (conf) {
182194
getClient(),
183195
conf.privateContainerName,
184196
normalisedPath,
185-
null,
186-
(err, res) => {
197+
null as unknown as azure.common.ContinuationToken,
198+
(err: Error | null, res: string[]) => {
187199
if (err) return callback(err);
188200
callback(null, _.uniq(res));
189201
}
190202
);
191203
};
192204

193-
const putDir = (dirInput, dirOutput, callback) => {
205+
const putDir = (dirInput: string, dirOutput: string, callback: any) => {
194206
nodeDir.paths(dirInput, (err, paths) => {
195207
async.each(
196208
paths.files,
@@ -213,12 +225,18 @@ module.exports = function (conf) {
213225
});
214226
};
215227

216-
const putFileContent = (fileContent, fileName, isPrivate, callback) => {
228+
const putFileContent = (
229+
fileContent: string | fs.ReadStream,
230+
fileName: string,
231+
isPrivate: boolean,
232+
callback: any
233+
) => {
217234
try {
218235
const fileInfo = getFileInfo(fileName);
219-
const contentSettings = {
220-
cacheControl: 'public, max-age=31556926'
221-
};
236+
const contentSettings: azure.BlobService.CreateBlockBlobRequestOptions['contentSettings'] =
237+
{
238+
cacheControl: 'public, max-age=31556926'
239+
};
222240
if (fileInfo.mimeType) {
223241
contentSettings.contentType = fileInfo.mimeType;
224242
}
@@ -227,7 +245,11 @@ module.exports = function (conf) {
227245
contentSettings.contentEncoding = 'gzip';
228246
}
229247

230-
const uploadToAzureContainer = (rereadable, containerName, cb) => {
248+
const uploadToAzureContainer = (
249+
rereadable: boolean,
250+
containerName: string,
251+
cb: any
252+
) => {
231253
try {
232254
if (fileContent instanceof stream.Stream) {
233255
return fileContent.pipe(
@@ -239,6 +261,8 @@ module.exports = function (conf) {
239261
if (rereadable) {
240262
// I need a fresh read stream and this is the only thing I came up with
241263
// very ugly and has poor performance, but works
264+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
265+
// @ts-ignore
242266
fileContent = getClient().createReadStream(
243267
containerName,
244268
fileName
@@ -267,7 +291,7 @@ module.exports = function (conf) {
267291
uploadToAzureContainer(
268292
makeReReadable,
269293
conf.privateContainerName,
270-
(err, result) => {
294+
(err: Error | null, result: any) => {
271295
if (err) {
272296
return callback(err);
273297
}
@@ -288,7 +312,12 @@ module.exports = function (conf) {
288312
}
289313
};
290314

291-
const putFile = (filePath, fileName, isPrivate, callback) => {
315+
const putFile = (
316+
filePath: string,
317+
fileName: string,
318+
isPrivate: boolean,
319+
callback: any
320+
) => {
292321
try {
293322
const stream = fs.createReadStream(filePath);
294323
putFileContent(stream, fileName, isPrivate, callback);
@@ -308,5 +337,7 @@ module.exports = function (conf) {
308337
putFileContent: fromCallback(putFileContent),
309338
adapterType: 'azure-blob-storage',
310339
isValid
311-
};
312-
};
340+
} as any;
341+
}
342+
343+
module.exports = azureAdapter;

0 commit comments

Comments
 (0)