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

Fix IMiddy and ICorsOptions type definitions #200

Merged
merged 4 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ declare var middy: {
declare namespace middy {
interface IMiddy extends Handler {
use: IMiddyUseFunction;
before: IMiddyMiddlewareFunction;
after: IMiddyMiddlewareFunction;
onError: IMiddyMiddlewareFunction;
before: (callbackFn: IMiddyMiddlewareFunction) => IMiddy;
after: (callbackFn: IMiddyMiddlewareFunction) => IMiddy;
onError: (callbackFn: IMiddyMiddlewareFunction) => IMiddy;
}

type IMiddyUseFunction = (config?: object) => IMiddy;
Expand Down
6 changes: 3 additions & 3 deletions middlewares.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { HttpError } from 'http-errors'
import middy from './'

interface ICorsOptions {
origin: string;
headers: string;
credentials: boolean;
origin?: string;
headers?: string;
credentials?: boolean;
}

interface ICacheOptions {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"types": "./index.d.ts",
"scripts": {
"test:lint": "eslint --ignore-pattern='node_modules/' --ignore-pattern='coverage/' --ignore-pattern='docs/' .",
"test:typings": "typings-tester --config tsconfig.json index.d.ts middlewares.d.ts",
"test:typings": "typings-tester --config tsconfig.json index.d.ts middlewares.d.ts src/__tests__/*.types.ts src/**/__tests__/*types.ts",
"test:unit": "jest --verbose --coverage",
"test:unit:watch": "jest --verbose --coverage --watch",
"test": "npm run test:lint && npm run test:unit",
"test": "npm run test:lint && npm run test:unit && npm run test:typings",
"build:readme": "jsdoc2md --global-index-format grouped --template README.md.hb src/* > README.md",
"build:docs": "jsdoc --readme README.md --package package.json --destination docs src",
"build": "npm run build:docs && npm run build:readme",
Expand All @@ -44,6 +44,8 @@
},
"homepage": "https://github.com/middyjs/middy#readme",
"devDependencies": {
"@types/jest": "^23.0.0",
"@types/node": "^10.3.1",
"aws-sdk": "^2.233.1",
"babel-jest": "^23.0.1",
"babel-preset-env": "^1.6.1",
Expand All @@ -60,7 +62,7 @@
"jsdoc-to-markdown": "^4.0.1",
"marked": "^0.3.12",
"regenerator-runtime": "^0.11.0",
"typescript": "^2.8.1",
"typescript": "^2.8.3",
"typings-tester": "^0.3.1"
},
"dependencies": {
Expand Down
47 changes: 47 additions & 0 deletions src/__tests__/middy.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import middy from '../../';

describe('🛵 Middy types test suite', () => {
test('"before" should take a function', () => {
const beforeMiddleware = jest.fn();

const handler = middy(jest.fn());

handler.before(beforeMiddleware);
})

test('"after" should take a function', () => {
const afterMiddleware = jest.fn();

const handler = middy(jest.fn());

handler.after(afterMiddleware);
})

test('"onError" should take a function', () => {
const errorMiddleware = jest.fn();

const handler = middy(jest.fn());

handler.onError(errorMiddleware);
})

test('middleware calls can be chained', () => {
const before = jest.fn()
const after = jest.fn()
const onError = jest.fn();

const middleware = () => ({
after,
before,
onError,
});

const handler = middy(jest.fn());

handler
.use(middleware)
.after(after)
.before(before)
.onError(onError);
})
})
35 changes: 35 additions & 0 deletions src/middlewares/__tests__/cors.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import middy from '../../../';
import { cors } from '../../../middlewares';

describe('📦 Middleware Types', () => {
describe('CORS', () => {
it('has an optional argument', () => {
const handler = middy(jest.fn());
handler.use(cors());
})

it ('has an optional origin field', () => {
const handler = middy(jest.fn());
handler.use(cors({
headers: "test-case",
credentials: true,
}));
})

it('has an optional headers field', () => {
const handler = middy(jest.fn());
handler.use(cors({
origin: 'example.com',
credentials: true,
}));
})

it('has an optional credentials field', () => {
const handler = middy(jest.fn());
handler.use(cors({
origin: 'example.com',
headers: "test-case",
}));
})
})
});
20 changes: 17 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es2015"],
"target": "es2015"
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"experimentalDecorators": true,
"importHelpers": true,
"lib": [
"es2018",
],
"moduleResolution": "node",
"noEmitHelpers": true,
"noEmitOnError": true,
"noImplicitReturns": true,
"target": "es5",
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason to change the target from es2015 to es5?

Copy link
Contributor Author

@ossareh ossareh Jun 7, 2018

Choose a reason for hiding this comment

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

oh, good catch. This was not meant to change.

Context: This branch started off as two features, this feature but then also another one which made the tests run on node v8.x - during that development I had an issue where the tests were not running on v6, I changed the value, it didn't make a difference, and I forgot to change it back.

The issue was that I'd switched the entire project over to using ts-jest at some point and it complained about some of the javascript within the project, but only on v6 - so I figured something about targeting or babel was wrong, and I was iterating through that.

I'll go ahead and change it back to es2015.

"sourceMap": true,
"strict": true,
},
"exclude": [
"node_modules",
],
"files": [
"index.d.ts",
"middlewares.d.ts"
Expand Down