Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add createTypedRouter for building statically typed api router #133

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ lib/
jest.config.js
jest.config.base.js
rollup.config.js
test-d/
.eslintrc.js
86 changes: 81 additions & 5 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,107 @@
// @ts-check

module.exports = {
extends: ['@thorgate/eslint-config-typescript'],
extends: [
'airbnb-base',
'airbnb-typescript/base',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:jest/recommended',
],
plugins: ['@typescript-eslint', 'jest', 'prettier'],
parser: '@typescript-eslint/parser',
parserOptions: {
project: ['./tsconfig.eslint.json', './packages/**/tsconfig.json'],
project: [
'./tsconfig.eslint.json',
'./packages/**/tsconfig.eslint.json',
],
},
env: {
es6: true,
browser: true,
'jest/globals': true,
node: true,
jest: true,
},
globals: {
DJ_CONST: false,
django: false,
},
rules: {
'prettier/prettier': 'error',

'no-underscore-dangle': 'off',
'max-classes-per-file': 'off',
'no-restricted-syntax': 'off',

// route templates use template format
'no-template-curly-in-string': 'off',

// https://basarat.gitbooks.io/typescript/docs/tips/defaultIsBad.html
'import/prefer-default-export': 'off',

// Set max line length to 140 chars. Prettier formats to 80.
// Using this rule so comments would be kept in more readable format
'max-len': ['warn', 140],

// else-return improves readability sometimes, especially with one-liners.
'no-else-return': 'warn',

// Allow unary ++ operator in for loop afterthoughts
'no-plusplus': 'off',

// Turn these into errors
'no-var': 'error',

// and disable these
'class-methods-use-this': 'off',

// Show TODOs and FIXMEs as warnings
'no-warning-comments': ['warn', { location: 'anywhere' }],

// Typescript configuration
'@typescript-eslint/explicit-module-boundary-types': 'off', // warn
'@typescript-eslint/camelcase': 'off',
'@typescript-eslint/naming-convention': 'off',
'@typescript-eslint/no-explicit-any': 'off', // warn
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{ argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
],

'react/no-unescaped-entities': 'off',
'react-hooks/rules-of-hooks': 'error',
'react-hooks/exhaustive-deps': 'warn',
// Automatically group import statements by type and sort them alphabetically.
"import/order": [
"error",
{
"groups": ["builtin", "external", "internal", "parent", "sibling", "index", "object", "type"],
"alphabetize": { "order": "asc", "caseInsensitive": true },
"newlines-between": "always"
}
],

// Keep imports in order
'sort-imports': [
'error',
{
ignoreCase: true,
ignoreDeclarationSort: true,
},
],

// jest - fail if no except in tests
'jest/expect-expect': [
'error',
{
assertFunctionNames: [
'expect',
'expectConfig',
'expectParentValidationError',
'expectError',
'expectResponse',
],
additionalTestBlockFunctions: [],
},
],
},
};
18 changes: 8 additions & 10 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,28 @@ jobs:
strategy:
fail-fast: false
matrix:
node: [14, 16, 18]
node: [18, 20]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
- uses: actions/cache@v2
- uses: actions/cache@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-node-${{ matrix.node }}-modules-${{ hashFiles('**/yarn.lock') }}
- name: Install deps
run: |
yarn
yarn bootstrap
run: yarn
- name: Build package
run: yarn build
- name: Run type-check
run: yarn type-check
- name: Run check-types
run: yarn check-types
- name: Run tests
run: yarn test:coverage
- name: Check examples
run: yarn run-examples
- name: Coveralls
if: matrix.node == '16'
if: matrix.node == '18'
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
4 changes: 2 additions & 2 deletions .template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "",
"main": "dist/index.js",
"module": "dist/<%PACKAGE_NAME_SAFE%>.esm.js",
"typings": "dist/index.d.ts",
"types": "dist/index.d.ts",
"react-native": "dist/<%PACKAGE_NAME_SAFE%>.esm.js",
"license": "MIT",
"author": "Thorgate <[email protected]>",
Expand All @@ -33,7 +33,7 @@
"scripts": {
"clean": "rimraf ./dist",
"lint": "eslint src -c ../../.eslintrc.js --ext ts --ext tsx",
"build": "tsdx build",
"build": "dts build",
"prebuild": "yarn clean && yarn lint"
}
}
2 changes: 1 addition & 1 deletion greenkeeper.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"packages/fetch-runtime/package.json",
"packages/is/package.json",
"packages/saga-router/package.json",
"packages/test-resource/package.json",
"packages/test-server/package.json",
"packages/test-utils/package.json",
"packages/tg-resource/package.json",
"packages/tg-resources-fetch/package.json",
"packages/tg-resources-superagent/package.json"
Expand Down
20 changes: 14 additions & 6 deletions jest.config.base.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
const config = {
verbose: true,
cache: false,

preset: 'ts-jest',

testMatch: ['<rootDir>/test/*.test.ts?(x)'],

globals: {
'ts-jest': {
tsconfig: {
sourceMap: true,
coveragePathIgnorePatterns: ['node_modules', '<rootDir>/test/'],

transform: {
'^.+\\.ts$': [
'ts-jest',
{
tsconfig: {
sourceMap: true,
},
useESM: true,
diagnostics: {
exclude: ['!**/*.(spec|test).ts'],
},
},
},
],
},
};

Expand Down
4 changes: 1 addition & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
bail: true,
verbose: true,
projects: ['<rootDir>/packages/*'],
preset: 'ts-jest',
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
};
14 changes: 4 additions & 10 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
{
"lerna": "3.20.2",
"packages": [
"examples/*",
"packages/*"
],
"packages": ["examples/*", "packages/*"],
"command": {
"publish": {
"ignoreChanges": [
"*.md"
]
"ignoreChanges": ["*.md"]
}
},
"npmClient": "yarn",
"useWorkspaces": true,
"version": "4.0.0-alpha.0"
"version": "4.0.0-alpha.0",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
54 changes: 26 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
"examples/*",
"packages/*"
],
"version": "3.3.0",
"version": "3.4.0.alpha1",
"description": "Abstractions on-top of `superagent` (or other Ajax libaries) for communication with REST.",
"scripts": {
"create-package": "node scripts/bootstrapPackage.js",
"bootstrap": "check-engines && lerna bootstrap",
"bootstrap": "check-engines",
"build": "lerna --ignore=examples/* exec -- yarn build",
"run-examples": "lerna exec --scope=run-examples yarn start",
"check-formatting": "prettier --check 'packages/**/src/**/*.{ts,tsx}' 'packages/**/test/**/*.{ts,tsx}'",
"code-format": "prettier --write 'packages/**/src/**/*.{ts,tsx}' 'packages/**/test/**/*.{ts,tsx}'",
"lint": "lerna --ignore=examples/* run lint",
"type-check": "lerna run type-check",
"test": "jest",
"fmt": "yarn",
"lint": "lerna --ignore=examples/* run lint -- --fix",
"check-types": "lerna run check-types",
"test": "jest --bail",
"test:watch": "jest --watch --bail",
"test:coverage": "yarn test --coverage",
"check-packages": "yarn clean && yarn build && yarn test && yarn lint && yarn type-check",
"check-packages": "yarn clean && yarn build && yarn test && yarn lint && yarn check-types",
"bump-version": "lerna --ignore=examples/* version --no-push --exact",
"deploy": "lerna publish --conventional-commits --message 'chore: release new versions'",
"prerelease": "lerna publish --conventional-prerelease --pre-dist-tag next",
Expand Down Expand Up @@ -56,46 +58,41 @@
"@babel/core": "^7.18.10",
"@babel/plugin-transform-async-to-generator": "^7.18.6",
"@babel/preset-env": "^7.12.1",
"@thorgate/eslint-config-typescript": "^3.1.0",
"@thorgate/prettier-config": "1.1.0",
"@types/cookie": "^0.5.1",
"@types/cookie-parser": "^1.4.1",
"@types/express": "^4.17.9",
"@types/jest": "^28.1.6",
"@types/jest": "^29.5.10",
"@types/morgan": "^1.9.3",
"@types/multer": "^1.4.7",
"@types/node": "^18.7.1",
"@types/qs": "^6.9.5",
"@types/superagent": "^4.1.10",
"@types/superagent": "^4.1.22",
"@types/uuid": "^8.3.4",
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"@typescript-eslint/eslint-plugin": "^6.7.2",
"@typescript-eslint/parser": "^6.7.2",
"abortcontroller-polyfill": "^1.7.5",
"babel-core": "^7.0.0-bridge.0",
"babel-jest": "28.1.3",
"babel-jest": "^29.7.0",
"check-engines": "1.5.0",
"cookie-parser": "^1.4.3",
"debug": "*",
"dtslint": "4.2.1",
"eslint": "^8.21.0",
"eslint": "^8.49.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-config-react-app": "^7.0.1",
"eslint-import-resolver-typescript": "^3.4.0",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-jest": "^26.8.2",
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-jest": "^27.6.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"express": "^4.17.1",
"form-data": "^4.0.0",
"fs-extra": "*",
"jest": "^28.1.3",
"jest-cli": "^28.1.3",
"jest-extended": "^3.0.2",
"lerna": "^5.4.0",
"jest": "^29.7.0",
"jest-cli": "^29.7.0",
"jest-extended": "^4.0.2",
"lerna": "^8.0.0",
"multiparty": "*",
"prettier": "^2.7.1",
"qs": "^6.10.1",
Expand All @@ -105,14 +102,15 @@
"rimraf": "^3.0.2",
"superagent": "^8.0.0",
"temp-dir": "2.0.0",
"ts-jest": "^28.0.7",
"tsdx": "^0.14.1",
"tslib": "^2.4.0",
"typescript": "^4.7.4",
"ts-jest": "^29.1.1",
"tsd": "^0.29.0",
"dts-cli": "^2.0.5",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"uuid": "^8.3.2"
},
"engines": {
"node": ">=10"
"node": ">=16"
},
"resolutions": {
"node-notifier": ">=10.0.0",
Expand Down
18 changes: 13 additions & 5 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"description": "Abstractions on-top of `superagent` (or other Ajax libaries) for communication with REST.",
"main": "dist/index.js",
"module": "dist/core.esm.js",
"typings": "dist/index.d.ts",
"types": "dist/index.d.ts",
"react-native": "dist/core.esm.js",
"license": "MIT",
"author": "Thorgate <[email protected]>",
Expand Down Expand Up @@ -37,14 +37,22 @@
],
"dependencies": {
"@tg-resources/is": "^3.4.0-alpha.0",
"@tg-resources/route-template": "^3.4.0-alpha.1",
"@tg-resources/route-template": "^4.0.0-alpha.0",
"cookie": ">=0.3.1"
},
"devDependencies": {
"jest-extended": "^4.0.2"
},
"tsd": {
"compilerOptions": {
"composite": false
}
},
"scripts": {
"clean": "rimraf ./dist",
"lint": "eslint src -c ../../.eslintrc.js --ext ts --ext tsx",
"type-check": "dtslint --localTs ../../node_modules/typescript/lib --expectOnly type-checks",
"build": "tsdx build",
"lint": "eslint src test -c ../../.eslintrc.js --ext ts --ext tsx",
"check-types": "tsd --show-diff",
"build": "dts build",
"prebuild": "yarn clean && yarn lint"
}
}
Loading
Loading