Skip to content

Commit

Permalink
feat: initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
glebbash committed Jan 14, 2022
0 parents commit cc1610e
Show file tree
Hide file tree
Showing 22 changed files with 20,430 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[*]
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 100
indent_size = 2

[*.md]
trim_trailing_whitespace = false
15 changes: 15 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
parser: '@typescript-eslint/parser'
extends:
- plugin:@typescript-eslint/recommended
- plugin:prettier/recommended

plugins:
- simple-import-sort

parserOptions:
ecmaVersion: 2018
sourceType: module

rules:
simple-import-sort/imports: error
simple-import-sort/exports: error
60 changes: 60 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: build
on: ["push", "pull_request"]

jobs:
test:
name: Build and test
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v1

- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14

- name: Test and generate coverage
run: |
npm ci
npm run test:prod
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-lcov: ./.coverage/lcov.info

release:
name: Release
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-20.04
needs: test
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Use Node.js 14
uses: actions/setup-node@v1
with:
node-version: 14

- name: Install dependencies
run: |
npm ci
npm run build
npm run build:docs
- name: Publish docs
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: gh-pages
publish_dir: docs

- name: Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
node_modules
.coverage
.nyc_output
.DS_Store
*.log
.vscode
.idea
dist
compiled
.awcache
.rpt2_cache
docs
4 changes: 4 additions & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

lint-staged
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run test:prod && npm run build
2 changes: 2 additions & 0 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
semi: true
singleQuote: true
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2022 glebbash <glebbash@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# typed-http-decorators

[![Deploy](https://github.com/glebbash/typed-http-decorators/workflows/build/badge.svg)](https://github.com/glebbash/typed-http-decorators/actions)
[![Coverage Status](https://coveralls.io/repos/github/glebbash/typed-http-decorators/badge.svg?branch=master)](https://coveralls.io/github/glebbash/typed-http-decorators?branch=master)

Typesafe decorators for HTTP endpoints

## Installation

```sh
npm i typed-http-decorators
```

## Usage

<!-- TODO: add better docs -->

Decorate endpoints:

```ts
// main.ts
import './overrides';

import { Method, NotFound, Ok } from './rest';

class NotFoundDto {
constructor(public message: string) {}
}

class ResourceDto {
constructor(public id: string, public name: string) {}
}

export class ResourceController {
@Method.Get('resource/:resourceId', {
permissions: ['resource.get'],
responses: [Ok.Type(ResourceDto), NotFound.Type(NotFoundDto)] as const,
})
async getResource(): Promise<Ok<ResourceDto> | NotFound<NotFoundDto>> {
return Ok(new ResourceDto('id', 'name'));
}
}
```

Specify endpoint decorator logic (you can apply Nest.js decorators for example):

```ts
// overrides.ts
import { setEndpointDecorator } from 'typed-http-decorators';

declare module './rest' {
interface EndpointOptions {
permissions: string[];
}
}

setEndpointDecorator((method, path, { permissions }) => (cls, endpointName) => {
// Endpoint decoration logic
console.log(
`Decorating ${cls.name}.${String(endpointName)}`,
`with route ${method} /${path} with permissions: ${permissions}`,
);
});
```

Bootstrapped with: [create-ts-lib-gh](https://github.com/glebbash/create-ts-lib-gh)

This project is [Mit Licensed](LICENSE).
24 changes: 24 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type Jest from '@jest/types';

const config: Jest.Config.InitialOptions = {
moduleFileExtensions: ['js', 'ts'],
rootDir: 'src',
testRegex: '.*\\.spec\\.ts$',
transform: {
'^.+\\.(t|j)s$': 'ts-jest',
},
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: 100,
},
},
collectCoverageFrom: ['**/*.ts'],
coveragePathIgnorePatterns: ['/node_modules/'],
coverageDirectory: '../.coverage',
testEnvironment: 'node',
};

export default config;
Loading

0 comments on commit cc1610e

Please sign in to comment.