diff --git a/packages/abstract/.npmignore b/packages/abstract/.npmignore new file mode 100644 index 000000000..c934a68a1 --- /dev/null +++ b/packages/abstract/.npmignore @@ -0,0 +1 @@ +lib/**/*.d.ts \ No newline at end of file diff --git a/packages/abstract/README.md b/packages/abstract/README.md new file mode 100644 index 000000000..d60e891d2 --- /dev/null +++ b/packages/abstract/README.md @@ -0,0 +1,22 @@ +# @qiwi/substrate-abstract +Abstract classes collection + +### Install +```bash +yarn add @qiwi/substrate-abstract -D +npm add @qiwi/substrate-abstract -D +``` + +### Usage +```typescript +import { AbstractError } from '@qiwi/substrate-abstract' + +class CustomError extends AbstractError { + foo: string + constructor(args) { + super(...args) + + this.foo = 'bar' + } +} +``` diff --git a/packages/abstract/jest.config.json b/packages/abstract/jest.config.json new file mode 100644 index 000000000..16f219ac0 --- /dev/null +++ b/packages/abstract/jest.config.json @@ -0,0 +1,30 @@ +{ + "testEnvironment": "node", + "collectCoverage": true, + "rootDir": "../../", + "transform": { + "^.+\\.tsx?$": "ts-jest" + }, + "collectCoverageFrom": [ + "/packages/abstract/src/main/**/*.ts" + ], + "testMatch": [ + "/packages/abstract/src/test/**/*.ts" + ], + "testPathIgnorePatterns": [ + "/packages/abstract/src/test/ts/index.ts", + "/packages/abstract/src/test/ts/index.d.ts" + ], + "transformIgnorePatterns": [ + "/!node_modules\\/lodash-es/", + "/node_modules/(?!lodash-es)" + ], + "moduleFileExtensions": [ + "ts", + "tsx", + "js", + "jsx", + "json", + "node" + ] +} diff --git a/packages/abstract/package.json b/packages/abstract/package.json new file mode 100644 index 000000000..0c845a08a --- /dev/null +++ b/packages/abstract/package.json @@ -0,0 +1,60 @@ +{ + "name": "@qiwi/substrate-abstract", + "description": "Abstract classes", + "version": "1.16.0", + "main": "target/es5/index.js", + "types": "typings/index.d.ts", + "typesVersions": { + ">=3.1": { + "*": [ + "typings/index.d.ts", + "src/test/ts/*" + ] + } + }, + "license": "MIT", + "scripts": { + "clean": "rm -rf target", + "build": "yarn build:es5 && yarn build:es6 && yarn build:ts && yarn libdef", + "build:es5": "mkdir -p target/es5 && tsc -p tsconfig.json --target ES5 --outDir target/es5", + "build:es6": "mkdir -p target/es6 && tsc -p tsconfig.json --target ES6 --outDir target/es6", + "build:ts": "cp -r src/main/ts/ target/ts/", + "libdef": "yarn dtsgen && yarn libdeffix && yarn flowgen", + "libdeffix": "node -r esm ../../scripts/libdef-fix.js --flow=./typings/index.flow.js --dts=./typings/index.d.ts", + "dtsgen": "dts-generator --project ./ --out typings/index.d.ts --prefix @qiwi/substrate-abstract/target/es5 --name @qiwi/substrate-abstract --main @qiwi/substrate-abstract/target/es5/index --moduleResolution node", + "flowgen": "flowgen typings/index.d.ts --output-file typings/index.flow.js", + "test": "yarn test:libdefs && yarn jest", + "test:libdefs": "cp typings/index.d.ts src/test/ts/index.d.ts && yarn dtslint", + "jest": "jest --config=jest.config.json", + "dtslint": "dtslint src/test/ts --onlyTestTsNext" + }, + "files": [ + "README.md", + "CHANGELOG.md", + "typings", + "target" + ], + "devDependencies": { + "@qiwi/uniconfig": "^3.0.1", + "@types/bluebird": "^3.5.27", + "@types/config": "^0.0.34", + "@types/jest": "^24.0.18", + "@types/lodash": "^4.14.137", + "@types/node": "^12.7.2", + "@types/parsimmon": "^1.10.0", + "@types/underscore": "^1.9.2", + "bluebird": "^3.5.5", + "conf": "^4.1.0", + "config": "^3.1.0", + "dts-generator": "^3.0.0", + "dtslint": "^0.9.1", + "flowgen": "^1.10.0", + "lodash": "^4.17.15", + "replace-in-file": "^4.1.3", + "ts-loader": "^6.0.4", + "ts-node": "^8.3.0", + "ts-to-flow": "^0.0.3", + "typescript": "3.5.3", + "underscore": "^1.9.1" + } +} diff --git a/packages/abstract/src/main/ts/AError.ts b/packages/abstract/src/main/ts/AError.ts new file mode 100644 index 000000000..394eb013d --- /dev/null +++ b/packages/abstract/src/main/ts/AError.ts @@ -0,0 +1,22 @@ +export type IError = { + message: string + code?: string | number + [key: string]: any +} + +export type IErrorOpts = { + message: string +} + +export class AbstractError extends Error implements IError { + message: string + code?: string | number + constructor(message: string | IErrorOpts) { + const opts = typeof message === 'string' + ? { message } + : message + + super(opts.message) + this.message = opts.message + } +} diff --git a/packages/abstract/src/main/ts/index.ts b/packages/abstract/src/main/ts/index.ts new file mode 100644 index 000000000..bd8aaade1 --- /dev/null +++ b/packages/abstract/src/main/ts/index.ts @@ -0,0 +1 @@ +export * from './AError' diff --git a/packages/abstract/src/test/ts/AError.ts b/packages/abstract/src/test/ts/AError.ts new file mode 100644 index 000000000..d86ca92cb --- /dev/null +++ b/packages/abstract/src/test/ts/AError.ts @@ -0,0 +1,12 @@ +import { AbstractError } from './index'; + +describe('AbstractError', () => { + describe('constructor', () => { + it('constructs by message', () => { + const message = 'foo'; + const instance = new AbstractError(message); + + expect(instance.message).toBe(message); + }); + }); +}); diff --git a/packages/abstract/src/test/ts/index.d.ts b/packages/abstract/src/test/ts/index.d.ts new file mode 100644 index 000000000..34fef3dcf --- /dev/null +++ b/packages/abstract/src/test/ts/index.d.ts @@ -0,0 +1,21 @@ +declare module '@qiwi/substrate-abstract/target/es5/AError' { + type IError = { + message: string; + code?: string | number; + [key: string]: any; + }; + type IErrorOpts = { + message: string; + }; + class AbstractError extends Error implements IError { + message: string; + code?: string | number; + constructor(message: string | IErrorOpts); + } +} +declare module '@qiwi/substrate-abstract/target/es5/index' { + export * from '@qiwi/substrate-abstract/target/es5/AError'; +} +declare module '@qiwi/substrate-abstract' { + export * from '@qiwi/substrate-abstract/target/es5/index'; +} diff --git a/packages/abstract/src/test/ts/index.ts b/packages/abstract/src/test/ts/index.ts new file mode 100644 index 000000000..c081d95e4 --- /dev/null +++ b/packages/abstract/src/test/ts/index.ts @@ -0,0 +1 @@ +export * from '@qiwi/substrate-abstract'; diff --git a/packages/abstract/src/test/ts/tsconfig.json b/packages/abstract/src/test/ts/tsconfig.json new file mode 100644 index 000000000..54d177d32 --- /dev/null +++ b/packages/abstract/src/test/ts/tsconfig.json @@ -0,0 +1,36 @@ +{ + "compilerOptions": { + "target": "es5", + "module": "commonjs", + "strict": true, + "noImplicitAny": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "noImplicitThis": true, + "alwaysStrict": true, + /* Additional Checks */ + /* next line commented out because we need unused vars for type tests */ + // "noUnusedLocals": true, + "noUnusedParameters": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true, + "lib": ["es6", "dom"], + "skipLibCheck": true, + "baseUrl": ".", + "paths": { + "@qiwi/substrate-abstract": [ + "../../main/ts" + ] + }, + "typeRoots": [ + ".", + "../../../../../node_modules/@types" + ] + }, + "include": [ + "./*" + ], + "exclude": [ + "node_modules" + ] +} diff --git a/packages/abstract/src/test/ts/tslint.json b/packages/abstract/src/test/ts/tslint.json new file mode 100644 index 000000000..43921e538 --- /dev/null +++ b/packages/abstract/src/test/ts/tslint.json @@ -0,0 +1,9 @@ +{ + "extends": "dtslint/dtslint.json", + "rules": { + "strict-type-predicates": true, + "interface-over-type-literal": false, + "interface-name": [true, "always-prefix"], + "max-line-length": [true, 1000] + } +} diff --git a/packages/abstract/tsconfig.json b/packages/abstract/tsconfig.json new file mode 100644 index 000000000..971a0d869 --- /dev/null +++ b/packages/abstract/tsconfig.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "src/main/ts", + "outDir": "target/es5", + "baseUrl": "src/main/ts" + }, + "include": [ + "src/main/**/*" + ] +} diff --git a/tsconfig.json b/tsconfig.json index 6cf7cae44..498ebe1ce 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,7 +6,9 @@ "@qiwi/substrate": ["./facade/src/main/ts"], "@qiwi/substrate/*": ["./facade/src/main/ts/*"], "@qiwi/substrate-types": ["./types/src/main/ts"], - "@qiwi/substrate-types/*": ["./types/src/main/ts/*"] + "@qiwi/substrate-types/*": ["./types/src/main/ts/*"], + "@qiwi/substrate-abstract": ["./abstract/src/main/ts"], + "@qiwi/substrate-abstract/*": ["./abstract/src/main/ts/*"] } } }