Skip to content

Commit

Permalink
fix: Stores as typescript and with interfaces. (#902)
Browse files Browse the repository at this point in the history
Co-authored-by: Ivar Conradi Østhus <ivarconr@gmail.com>
  • Loading branch information
Christopher Kolstad and ivarconr committed Aug 12, 2021
1 parent c5b8b2f commit ff7be76
Show file tree
Hide file tree
Showing 381 changed files with 7,435 additions and 5,098 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -8,3 +8,4 @@ website/translated_docs
website/core
website/pages
websitev2
setupJest.js
12 changes: 8 additions & 4 deletions .eslintrc
Expand Up @@ -5,18 +5,17 @@
},
"extends": [
"airbnb-typescript/base",
"prettier"
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2019,
"project": "./tsconfig.json"
},
"plugins": ["prettier","@typescript-eslint"],
"plugins": ["@typescript-eslint","prettier"],
"root": true,
"rules": {
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/indent": ["error", 4],
"@typescript-eslint/naming-convention": 0,
"@typescript-eslint/space-before-function-paren": 0,
"import/prefer-default-export": 0,
Expand Down Expand Up @@ -46,10 +45,15 @@
"files": ["*.ts", "*.tsx"],
"rules": {
"@typescript-eslint/explicit-module-boundary-types": ["error"],
"@typescript-eslint/indent": ["error"],
"@typescript-eslint/naming-convention": ["error"],
"@typescript-eslint/space-before-function-paren": ["error"]
}
},
{
"files": ["src/test/e2e/helpers/test-helper.ts"],
"rules": {
"import/no-extraneous-dependencies": "off"
}
}
],
"ignorePatterns": ["**/docs/api/oas/", "examples/**"]
Expand Down
28 changes: 19 additions & 9 deletions package.json
Expand Up @@ -19,6 +19,7 @@
"bugs": {
"url": "https://github.com/unleash/unleash/issues"
},
"types": "./dist/lib/types/index.d.js",
"engines": {
"node": ">=14"
},
Expand All @@ -43,6 +44,10 @@
"clean": "del-cli --force dist"
},
"jest": {
"automock": false,
"setupFiles": [
"./setupJest.js"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
Expand Down Expand Up @@ -109,37 +114,42 @@
"devDependencies": {
"@types/bcrypt": "^5.0.0",
"@types/express": "^4.17.11",
"@types/express-session": "^1.17.4",
"@types/jest": "^26.0.23",
"@types/js-yaml": "^3.12.7",
"@types/memoizee": "^0.4.6",
"@types/node": "^15.6.0",
"@types/node-fetch": "^2.5.10",
"@types/nodemailer": "^6.4.1",
"@types/owasp-password-strength-test": "^1.3.0",
"@types/stoppable": "^1.1.1",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"copyfiles": "^2.4.1",
"coveralls": "^3.1.0",
"del-cli": "^4.0.1",
"eslint": "^6.8.0",
"eslint": "^7.32.0",
"eslint-config-airbnb-base": "^14.2.1",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-prettier": "^3.3.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-import": "^2.23.4",
"eslint-plugin-prettier": "^3.4.0",
"faker": "^5.5.3",
"fetch-mock": "^9.11.0",
"husky": "^4.2.3",
"jest": "^27.0.1",
"jest": "^27.0.6",
"jest-fetch-mock": "^3.0.3",
"lint-staged": "^11.0.0",
"prettier": "^1.19.1",
"prettier": "^2.3.2",
"proxyquire": "^2.1.3",
"source-map-support": "^0.5.19",
"superagent": "^6.1.0",
"supertest": "^6.1.3",
"ts-jest": "^27.0.0",
"ts-node": "^10.0.0",
"ts-jest": "^27.0.4",
"ts-node": "^10.1.0",
"tsc-watch": "^4.4.0",
"typescript": "^4.2.4"
"typescript": "^4.3.5"
},
"resolutions": {
"set-value": "^2.0.1",
Expand Down
2 changes: 2 additions & 0 deletions setupJest.js
@@ -0,0 +1,2 @@
// adds the 'fetchMock' global variable and rewires 'fetch' global to call 'fetchMock' instead of the real implementation
require('jest-fetch-mock').enableMocks();
11 changes: 0 additions & 11 deletions snapshots/dist/lib/addons/slack.test.js.md

This file was deleted.

17 changes: 0 additions & 17 deletions snapshots/src/lib/addons/datadog.test.js.md

This file was deleted.

17 changes: 0 additions & 17 deletions snapshots/src/lib/addons/slack.test.js.md

This file was deleted.

17 changes: 0 additions & 17 deletions snapshots/src/lib/addons/teams.test.js.md

This file was deleted.

22 changes: 6 additions & 16 deletions src/lib/addons/addon-schema.js → src/lib/addons/addon-schema.ts
@@ -1,8 +1,8 @@
const joi = require('joi');
const { nameType } = require('../routes/admin-api/util');
const { tagTypeSchema } = require('../services/tag-type-schema');
import joi from 'joi';
import { nameType } from '../routes/admin-api/util';
import { tagTypeSchema } from '../services/tag-type-schema';

const addonDefinitionSchema = joi.object().keys({
export const addonDefinitionSchema = joi.object().keys({
name: nameType,
displayName: joi.string(),
documentationUrl: joi.string().uri({ scheme: [/https?/] }),
Expand All @@ -21,16 +21,6 @@ const addonDefinitionSchema = joi.object().keys({
sensitive: joi.boolean().default(false),
}),
),
events: joi
.array()
.optional()
.items(joi.string()),
tagTypes: joi
.array()
.optional()
.items(tagTypeSchema),
events: joi.array().optional().items(joi.string()),
tagTypes: joi.array().optional().items(tagTypeSchema),
});

module.exports = {
addonDefinitionSchema,
};
71 changes: 0 additions & 71 deletions src/lib/addons/addon.test.js

This file was deleted.

40 changes: 40 additions & 0 deletions src/lib/addons/addon.test.ts
@@ -0,0 +1,40 @@
import fetchMock from 'jest-fetch-mock';
import noLogger from '../../test/fixtures/no-logger';

import SlackAddon from './slack';

beforeEach(() => {
fetchMock.resetMocks();
});

test('Retries if fetch throws', async () => {
const url = 'https://test.some.com';
jest.useFakeTimers('modern');
const addon = new SlackAddon({
getLogger: noLogger,
unleashUrl: url,
});
fetchMock.mockIf('https://test.some.com', 'OK', {
status: 201,
statusText: 'ACCEPTED',
});
await addon.fetchRetry(url);
jest.advanceTimersByTime(1000);
jest.useRealTimers();
});

test('does not crash even if fetch throws', async () => {
const url = 'https://test.some.com';
jest.useFakeTimers('modern');
const addon = new SlackAddon({
getLogger: noLogger,
unleashUrl: url,
});
fetchMock.mockResponse(() => {
throw new Error('Network error');
});
await addon.fetchRetry(url);
jest.advanceTimersByTime(1000);
expect(fetchMock.mock.calls).toHaveLength(2);
jest.useRealTimers();
});
36 changes: 26 additions & 10 deletions src/lib/addons/addon.js → src/lib/addons/addon.ts
@@ -1,10 +1,20 @@
'use strict';
import fetch, { Response } from 'node-fetch';
import { addonDefinitionSchema } from './addon-schema';
import { IUnleashConfig } from '../types/option';
import { Logger } from '../logger';
import { IAddonDefinition, IEvent } from '../types/model';

const fetch = require('node-fetch');
const { addonDefinitionSchema } = require('./addon-schema');
export default abstract class Addon {
logger: Logger;

class Addon {
constructor(definition, { getLogger }) {
_name: string;

_definition: IAddonDefinition;

constructor(
definition: IAddonDefinition,
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
) {
this.logger = getLogger(`addon/${definition.name}`);
const { error } = addonDefinitionSchema.validate(definition);
if (error) {
Expand All @@ -18,15 +28,20 @@ class Addon {
this._definition = definition;
}

get name() {
get name(): string {
return this._name;
}

get definition() {
get definition(): IAddonDefinition {
return this._definition;
}

async fetchRetry(url, options = {}, retries = 1, backoff = 300) {
async fetchRetry(
url: string,
options = {},
retries: number = 1,
backoff: number = 300,
): Promise<Response> {
const retryCodes = [408, 500, 502, 503, 504, 522, 524];
let res;
try {
Expand All @@ -45,6 +60,7 @@ class Addon {
}
return res;
}
}

module.exports = Addon;
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
abstract handleEvent(event: IEvent, parameters: any): Promise<void>;
}

0 comments on commit ff7be76

Please sign in to comment.