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(core): add support for esm #69

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 36 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"cz-conventional-changelog": "^3.1.0",
"lerna": "^3.20.2",
"metapak": "^3.1.8",
"metapak-nfroidure": "10.0.1"
"metapak-nfroidure": "10.1.2"
},
"contributors": [],
"engines": {
Expand Down
37 changes: 37 additions & 0 deletions packages/whook-authorization/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 47 additions & 15 deletions packages/whook-authorization/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"wrapper",
"authorization"
],
"main": "dist/index.js",
"main": "dist/index",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
"metapak": {
"configs": [
Expand Down Expand Up @@ -54,30 +55,30 @@
"homepage": "https://github.com/nfroidure/whook",
"dependencies": {
"@whook/whook": "^3.1.3",
"common-services": "^7.0.0",
"common-services": "^7.1.1",
"http-auth-utils": "^2.4.0",
"knifecycle": "^9.0.0"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
"@babel/core": "^7.8.7",
"@babel/core": "^7.9.0",
"@babel/plugin-proposal-class-properties": "^7.8.3",
"@babel/plugin-proposal-object-rest-spread": "^7.8.3",
"@babel/preset-env": "^7.8.7",
"@babel/preset-typescript": "^7.8.3",
"@babel/register": "^7.8.6",
"@types/jest": "^24.9.0",
"@typescript-eslint/eslint-plugin": "^2.24.0",
"@typescript-eslint/parser": "^2.24.0",
"@babel/plugin-proposal-object-rest-spread": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@babel/preset-typescript": "^7.9.0",
"@babel/register": "^7.9.0",
"@types/jest": "^25.1.4",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0",
"babel-eslint": "^10.1.0",
"babel-plugin-knifecycle": "^1.2.0",
"eslint": "^6.8.0",
"eslint-plugin-prettier": "^3.1.2",
"jest": "^25.1.0",
"jest": "^25.2.4",
"jsdoc-to-markdown": "^5.0.3",
"metapak": "^3.1.8",
"metapak-nfroidure": "10.0.1",
"prettier": "^1.19.1",
"metapak-nfroidure": "10.1.2",
"prettier": "^2.0.2",
"rimraf": "^3.0.2",
"typescript": "^3.8.3"
},
Expand Down Expand Up @@ -154,7 +155,36 @@
"@babel/proposal-class-properties",
"@babel/plugin-proposal-object-rest-spread",
"babel-plugin-knifecycle"
]
],
"env": {
"cjs": {
"presets": [
[
"@babel/env",
{
"targets": {
"node": "10"
},
"modules": "commonjs"
}
]
]
},
"mjs": {
"presets": [
[
"@babel/env",
{
"targets": {
"node": "12"
},
"modules": "false"
}
]
]
}
},
"sourceMaps": true
},
"jest": {
"coverageReporters": [
Expand All @@ -170,7 +200,9 @@
},
"scripts": {
"cli": "env NODE_ENV=${NODE_ENV:-cli}",
"compile": "babel --extensions '.ts,.js' src --out-dir=dist --source-maps=true",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
"compile:cjs": "babel --env-name=cjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
"compile:mjs": "babel --env-name=mjs --out-file-extension=.mjs --out-dir=dist --extensions '.ts,.js' --source-maps=true src",
"cover": "npm run jest -- --coverage",
"doc": "echo \"# API\" > API.md; jsdoc2md 'dist/**/*.js' >> API.md && git add API.md",
"jest": "NODE_ENV=test jest",
Expand Down
14 changes: 7 additions & 7 deletions packages/whook-authorization/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { wrapHandlerWithAuthorization } from '.';
import {
handler,
ServiceInitializer,
HandlerInitializer,
HandlerFunction,
} from 'knifecycle';
import { handler } from 'knifecycle';
import YError from 'yerror';
import {
BEARER as BEARER_MECHANISM,
BASIC as BASIC_MECHANISM,
} from 'http-auth-utils';
import { WhookOperation, WhookHandler } from '@whook/whook';
import type {
ServiceInitializer,
HandlerInitializer,
HandlerFunction,
} from 'knifecycle';
import type { WhookOperation, WhookHandler } from '@whook/whook';

describe('wrapHandlerWithAuthorization', () => {
const noopMock = (jest.fn(() => ({
Expand Down
37 changes: 15 additions & 22 deletions packages/whook-authorization/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import {
SPECIAL_PROPS,
reuseSpecialProps,
alsoInject,
ServiceInitializer,
Parameters,
Dependencies,
} from 'knifecycle';
import { SPECIAL_PROPS, reuseSpecialProps, alsoInject } from 'knifecycle';
import HTTPError from 'yhttperror';
import {
WhookHandler,
WhookOperation,
WhookErrorsDescriptors,
DEFAULT_ERROR_URI,
DEFAULT_HELP_URI,
} from '@whook/whook';
import { LogService } from 'common-services';
import { DEFAULT_ERROR_URI, DEFAULT_HELP_URI } from '@whook/whook';
import {
parseAuthorizationHeader,
buildWWWAuthenticateHeader,
BEARER as BEARER_MECHANISM,
} from 'http-auth-utils';
import type { ServiceInitializer, Parameters, Dependencies } from 'knifecycle';
import type {
WhookHandler,
WhookOperation,
WhookErrorsDescriptors,
} from '@whook/whook';
import type { LogService } from 'common-services';

export const AUTHORIZATION_ERRORS_DESCRIPTORS: WhookErrorsDescriptors = {
E_OPERATION_REQUIRED: {
Expand Down Expand Up @@ -164,7 +157,7 @@ async function handleWithAuthorization<
'undefined' === typeof operation.security ||
operation.security.length === 0;
const optionalAuth = (operation.security || []).some(
security => Object.keys(security).length === 0,
(security) => Object.keys(security).length === 0,
);
const authorization =
parameters.access_token && DEFAULT_MECHANISM
Expand All @@ -185,9 +178,9 @@ async function handleWithAuthorization<
} else {
let parsedAuthorization;

const usableMechanisms = MECHANISMS.filter(mechanism =>
const usableMechanisms = MECHANISMS.filter((mechanism) =>
operation.security.find(
security => security[`${mechanism.type.toLowerCase()}Auth`],
(security) => security[`${mechanism.type.toLowerCase()}Auth`],
),
);

Expand All @@ -208,7 +201,7 @@ async function handleWithAuthorization<
if (
err.code === 'E_UNKNOWN_AUTH_MECHANISM' &&
MECHANISMS.some(
mechanism =>
(mechanism) =>
authorization.substr(0, mechanism.type.length) === mechanism.type,
)
) {
Expand All @@ -219,7 +212,7 @@ async function handleWithAuthorization<

const authName = `${parsedAuthorization.type.toLowerCase()}Auth`;
const requiredScopes = (operation.security.find(
security => security[authName],
(security) => security[authName],
) || { [authName]: [] })[authName];

// If security exists, we need at least one scope
Expand All @@ -246,7 +239,7 @@ async function handleWithAuthorization<

// Check scopes
if (
!requiredScopes.some(requiredScope =>
!requiredScopes.some((requiredScope) =>
authenticationData.scope.split(',').includes(requiredScope),
)
) {
Expand Down
Loading