Skip to content

Commit

Permalink
Merge pull request #468 from instantcommerce/beta
Browse files Browse the repository at this point in the history
v2.0.0
  • Loading branch information
ggurkal committed Aug 18, 2022
2 parents 224c338 + 4d8010a commit 2b28280
Show file tree
Hide file tree
Showing 47 changed files with 481 additions and 309 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 Story of AMS
Copyright (c) 2021 Instant Commerce

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<div align="center">
<a aria-label="Story of AMS logo" href="https://storyofams.com/" target="_blank" align="center">
<img src="https://avatars.githubusercontent.com/u/19343504" alt="Story of AMS" width="100">
<a aria-label="Instant Commerce logo" href="https://instantcommerce.io/" target="_blank" align="center">
<img src="https://avatars.githubusercontent.com/u/93975473" alt="Instant Commerce" width="100">
</a>
<h1 align="center">@storyofams/next-api-decorators</h1>
<h1 align="center">next-api-decorators</h1>
<p align="center">
<a aria-label="releases" href="https://GitHub.com/storyofams/next-api-decorators/releases/" target="_blank">
<img src="https://github.com/storyofams/next-api-decorators/workflows/Release/badge.svg">
<a aria-label="releases" href="https://github.com/instantcommerce/next-api-decorators/releases/" target="_blank">
<img src="https://github.com/instantcommerce/next-api-decorators/workflows/Release/badge.svg">
</a>
<a aria-label="npm" href="https://www.npmjs.com/package/@storyofams/next-api-decorators" target="_blank">
<img src="https://img.shields.io/npm/v/@storyofams/next-api-decorators">
<a aria-label="npm" href="https://www.npmjs.com/package/next-api-decorators" target="_blank">
<img src="https://img.shields.io/npm/v/next-api-decorators">
</a>
<a aria-label="codecov" href="https://codecov.io/gh/storyofams/next-api-decorators" target="_blank">
<img src="https://codecov.io/gh/storyofams/next-api-decorators/branch/master/graph/badge.svg?token=ZV0YT4HU5H">
<a aria-label="codecov" href="https://codecov.io/gh/instantcommerce/next-api-decorators" target="_blank">
<img src="https://codecov.io/gh/instantcommerce/next-api-decorators/branch/master/graph/badge.svg?token=ZV0YT4HU5H">
</a>
<a aria-label="stars" href="https://github.com/storyofams/next-api-decorators/stargazers/" target="_blank">
<img src="https://img.shields.io/github/stars/storyofams/next-api-decorators.svg?style=social&label=Star&maxAge=86400" />
<a aria-label="stars" href="https://github.com/instantcommerce/next-api-decorators/stargazers/" target="_blank">
<img src="https://img.shields.io/github/stars/instantcommerce/next-api-decorators.svg?style=social&label=Star&maxAge=86400" />
</a>
</p>
</div>
Expand Down
4 changes: 2 additions & 2 deletions examples/route-matching/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# @storyofams/next-api-decorators/example
# next-api-decorators/example

Example usage of the `@storyofams/next-api-decorators` with Next.js
Example usage of the `next-api-decorators` with Next.js

## Getting started

Expand Down
2 changes: 1 addition & 1 deletion examples/route-matching/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type-check": "tsc"
},
"dependencies": {
"@storyofams/next-api-decorators": "../../dist",
"next-api-decorators": "../../dist",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"next": "^12.1.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/route-matching/pages/api/postman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Download,
DownloadFileResult,
createHandler,
} from '@storyofams/next-api-decorators';
} from 'next-api-decorators';

class Postman {
private pathToFile = `${process.cwd()}/postman.json`;
Expand Down
2 changes: 1 addition & 1 deletion examples/route-matching/pages/api/users/[[...params]].ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ParseNumberPipe,
DefaultValuePipe,
NotFoundException,
} from '@storyofams/next-api-decorators';
} from 'next-api-decorators';
import { User, sampleUserData } from '../../../data';
import { CreateUserInput, UpdateUserInput } from '../../../dto';

Expand Down
2 changes: 1 addition & 1 deletion examples/route-matching/postman.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"name": "@storyofams/next-api-decorators/example",
"name": "next-api-decorators/example",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
Expand Down
6 changes: 3 additions & 3 deletions examples/route-matching/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@
minimatch "^3.0.4"
strip-json-comments "^3.1.1"

"next-api-decorators@../../dist":
version "0.0.0"

"@mdx-js/util@1.6.22":
version "1.6.22"
resolved "https://registry.yarnpkg.com/@mdx-js/util/-/util-1.6.22.tgz#219dfd89ae5b97a8801f015323ffa4b62f45718b"
Expand Down Expand Up @@ -390,9 +393,6 @@
resolved "https://registry.yarnpkg.com/@storyofams/eslint-config-ams/-/eslint-config-ams-1.1.3.tgz#ee6de44c89c6fabe401e6883f8b08ccc11fdc538"
integrity sha512-iygvgZQIu8CicKYzXdHfEfrGXf23BeSeHrIDdj+zl0f85Al1lPINTC90y9xtQdbKKgw9iXrlmuiaJIjL1o1Y1w==

"@storyofams/next-api-decorators@../../dist":
version "0.0.0"

"@types/json5@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
Expand Down
4 changes: 2 additions & 2 deletions examples/with-next-auth/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `@storyofams/next-api-decorators` example with `next-auth`
# `next-api-decorators` example with `next-auth`

Example usage of the `@storyofams/next-api-decorators` with `next-auth`. This example only covers logging in via Github, for more providers please refer to `next-auth` documentation [here](https://next-auth.js.org/configuration/providers).
Example usage of the `next-api-decorators` with `next-auth`. This example only covers logging in via Github, for more providers please refer to `next-auth` documentation [here](https://next-auth.js.org/configuration/providers).

## Getting started

Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type-check": "tsc"
},
"dependencies": {
"@storyofams/next-api-decorators": "../../dist",
"next-api-decorators": "../../dist",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"next": "^12.1.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-auth/pages/api/users/[[...params]].ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
NextFunction,
UnauthorizedException,
SetHeader,
} from '@storyofams/next-api-decorators';
} from 'next-api-decorators';
import { NextApiRequest, NextApiResponse } from 'next';
import { getToken } from 'next-auth/jwt';
import { User, sampleUserData } from '../../../data';
Expand Down
2 changes: 1 addition & 1 deletion examples/with-next-auth/postman.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"info": {
"name": "@storyofams/next-api-decorators/example",
"name": "next-api-decorators/example",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
Expand Down
6 changes: 3 additions & 3 deletions examples/with-next-auth/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@
minimatch "^3.0.4"
strip-json-comments "^3.1.1"

"next-api-decorators@../../dist":
version "0.0.0"

"@mdx-js/util@1.6.22":
version "1.6.22"
resolved "https://registry.yarnpkg.com/@mdx-js/util/-/util-1.6.22.tgz#219dfd89ae5b97a8801f015323ffa4b62f45718b"
Expand Down Expand Up @@ -395,9 +398,6 @@
resolved "https://registry.yarnpkg.com/@storyofams/eslint-config-ams/-/eslint-config-ams-1.1.3.tgz#ee6de44c89c6fabe401e6883f8b08ccc11fdc538"
integrity sha512-iygvgZQIu8CicKYzXdHfEfrGXf23BeSeHrIDdj+zl0f85Al1lPINTC90y9xtQdbKKgw9iXrlmuiaJIjL1o1Y1w==

"@storyofams/next-api-decorators@../../dist":
version "0.0.0"

"@types/json5@^0.0.29":
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
Expand Down
8 changes: 6 additions & 2 deletions lib/createHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'reflect-metadata';
import express from 'express';
import request from 'supertest';
import { createHandler } from './createHandler';
import { HttpVerb, HTTP_METHOD_TOKEN } from './decorators';
import { HttpMethod, HTTP_METHOD_TOKEN } from './decorators';

describe('createHandler', () => {
it('Should return not found response when "req.url" is undefined', () => {
Expand All @@ -22,7 +22,11 @@ describe('createHandler', () => {

it('Should return not found when method is not found.', done => {
class TestHandler {}
Reflect.defineMetadata(HTTP_METHOD_TOKEN, [{ path: '/', verb: HttpVerb.GET, propertyKey: 'index' }], TestHandler);
Reflect.defineMetadata(
HTTP_METHOD_TOKEN,
[{ path: '/', method: HttpMethod.GET, propertyKey: 'index' }],
TestHandler
);

const server = express();
server.use(express.json());
Expand Down
2 changes: 1 addition & 1 deletion lib/createHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { parseRequestUrl } from './internals/parseRequestUrl';
*
* @example
* ```ts
* import { createHandler, Get } from '@storyofams/next-api-decorators';
* import { createHandler, Get } from 'next-api-decorators';
*
* class Events {
* Get()
Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/catch.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ClassConstructor } from 'class-transformer';
import type { NextApiRequest, NextApiResponse } from 'next';

export const CATCH_TOKEN = Symbol('ams:next:catch');
export const CATCH_TOKEN = Symbol('instant:next:catch');

type ExceptionHandlerFunction<T> = (error: T, req: NextApiRequest, res: NextApiResponse) => void | Promise<void>;

Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/download.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const HTTP_DOWNLOAD_TOKEN = Symbol('ams:next:download');
export const HTTP_DOWNLOAD_TOKEN = Symbol('instant:next:download');

/**
* Marks the method as a download handler for the client, so the returned file can be downloaded by the browser.
Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/httpCode.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const HTTP_CODE_TOKEN = Symbol('ams:next:httpCode');
export const HTTP_CODE_TOKEN = Symbol('instant:next:httpCode');

/**
* Defines the HTTP response code of the route.
Expand Down
30 changes: 17 additions & 13 deletions lib/decorators/httpMethod.decorators.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import 'reflect-metadata';
import * as lp from '../internals/loadPackage';
import { Get, Post, Put, Delete, HttpVerb, HTTP_METHOD_TOKEN, Patch } from './httpMethod.decorators';
import { Get, Post, Put, Delete, HttpMethod, HTTP_METHOD_TOKEN, Patch } from './httpMethod.decorators';

class Test {
@Get()
Expand Down Expand Up @@ -49,30 +49,34 @@ describe('HttpMethod decorator', () => {
process.env = ENV;
});

it('Should create all the verbs.', () => {
it('Should create all the methods.', () => {
const meta = Reflect.getMetadata(HTTP_METHOD_TOKEN, Test);
expect(meta).toBeInstanceOf(Array);
expect(meta).toMatchObject(
expect.arrayContaining([
{ path: '/', verb: HttpVerb.GET, propertyKey: 'get' },
{ path: '/', verb: HttpVerb.POST, propertyKey: 'post' },
{ path: '/', verb: HttpVerb.PUT, propertyKey: 'put' },
{ path: '/', verb: HttpVerb.DELETE, propertyKey: 'delete' },
{ path: '/', verb: HttpVerb.PATCH, propertyKey: 'patch' }
{ path: '/', method: HttpMethod.GET, propertyKey: 'get' },
{ path: '/', method: HttpMethod.POST, propertyKey: 'post' },
{ path: '/', method: HttpMethod.PUT, propertyKey: 'put' },
{ path: '/', method: HttpMethod.DELETE, propertyKey: 'delete' },
{ path: '/', method: HttpMethod.PATCH, propertyKey: 'patch' }
])
);
});

it('Should create the GET verb with paths', () => {
it('Should create the GET method with paths', () => {
const meta = Reflect.getMetadata(HTTP_METHOD_TOKEN, TestPath);
expect(meta).toBeInstanceOf(Array);
expect(meta).toMatchObject(
expect.arrayContaining([
{ path: '/', verb: HttpVerb.GET, propertyKey: 'index' },
{ path: '/explore', verb: HttpVerb.GET, propertyKey: 'explore' },
{ path: '/explore/:id', verb: HttpVerb.GET, propertyKey: 'exploreDetails' },
{ path: '/explore/:id/comments', verb: HttpVerb.GET, propertyKey: 'exploreDetailsComments' },
{ path: '/explore/:id/comments/:commentId', verb: HttpVerb.GET, propertyKey: 'exploreDetailsCommentDetails' }
{ path: '/', method: HttpMethod.GET, propertyKey: 'index' },
{ path: '/explore', method: HttpMethod.GET, propertyKey: 'explore' },
{ path: '/explore/:id', method: HttpMethod.GET, propertyKey: 'exploreDetails' },
{ path: '/explore/:id/comments', method: HttpMethod.GET, propertyKey: 'exploreDetailsComments' },
{
path: '/explore/:id/comments/:commentId',
method: HttpMethod.GET,
propertyKey: 'exploreDetailsCommentDetails'
}
])
);
});
Expand Down
Loading

1 comment on commit 2b28280

@vercel
Copy link

@vercel vercel bot commented on 2b28280 Aug 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.