16.1.0 • Published 2 months ago

@whook/cors v16.1.0

Weekly downloads
22
License
MIT
Repository
github
Last release
2 months ago

@whook/cors

A wrapper to provide CORS support to a Whook server

GitHub license

This Whook wrapper provides CORS support by adding it to your OpenAPI file and creating the handlers that runs the OPTIONS method when you cannot do it at the proxy/gateway level.

Usage

To use this plugin, simply install it:

npm i @whook/cors;

Declare it in the src/index.ts file of your project:

  // ...

  $.register(
    constant('HANDLERS_WRAPPERS', [
+      'wrapHandlerWithCORS',
      'wrapHandlerWithAuthorization',
    ]),
  );

  // ...

  $.register(
    constant('WHOOK_PLUGINS', [
      ...WHOOK_DEFAULT_PLUGINS,
+      '@whook/cors',
      '@whook/authorization',
    ]),
  );

  // ...

Declare types in your src/whook.d.ts definition:

+ import type {
+   CORSConfig,
+   WhookAPIOperationCORSConfig,
+ } from '@whook/cors';

declare module 'application-services' {

  // ...

  export interface AppConfig
-    extends WhookBaseConfigs {}
+    extends WhookBaseConfigs, CORSConfig {}

  // ...
}

// ...

declare module '@whook/whook' {
  export interface WhookAPIHandlerDefinition<
    T extends Record<string, unknown> = Record<string, unknown>,
    U extends {
      [K in keyof U]: K extends `x-${string}` ? Record<string, unknown> : never;
    } = unknown,
    V extends Record<string, unknown> = Record<string, unknown>,
  > extends WhookBaseAPIHandlerDefinition<T, U> {
    operation: U & WhookAPIOperation<
        T &
      WhookAPIOperationSwaggerConfig & WhookAPIOperationCORSConfig
    >;
  }
}

And add the CORS config (usually in src/config/common/config.js):

// ...
import type { AppConfig } from 'application-services';

const CONFIG: AppConfig = {
  // ...
+   CORS: {
+     'Access-Control-Allow-Origin': '*',
+     'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',
+     'Access-Control-Allow-Headers': [
+       'Accept',
+       'Accept-Encoding',
+       'Accept-Language',
+       'Referrer',
+       'Content-Type',
+       'Content-Encoding',
+       'Authorization',
+       'Keep-Alive',
+       'User-Agent',
+     ].join(','),
+   },
};

export default CONFIG;

You should also use the wrapped error handler:

+ import { initErrorHandlerWithCORS } from '@whook/cors';

// ...

export async function prepareEnvironment<T extends Knifecycle<Dependencies>>(
    $: T = new Knifecycle() as T,
  ): Promise<T> {

//...

+  // Add the CORS wrapped error handler
+  $.register(initErrorHandlerWithCORS);

  return $;
}

According to the kind of build you use, you may also declare it in your src/build.ts file:

  $.register(
    constant('INITIALIZER_PATH_MAP', {
      ...DEFAULT_BUILD_INITIALIZER_PATH_MAP,
      // MY_SERVICE: '@my/service_module_name',
      jwtToken: 'jwt-service/dist/index.js',
+      errorHandler: '@whook/cors/dist/services/errorHandler.js',
    }),
  );

Finally, you must adapt the API service to handle CORS options:

+ import { augmentAPIWithCORS } from '@whook/cors';

// (...)

export default name('API', autoService(initAPI));

// The API service is where you put your handlers
// altogether to form the final API
async function initAPI({
// (..)
) {

  // (..)

  // You can apply transformations to your API like
  // here for CORS support (OPTIONS method handling)
-  return augmentAPIWithFakeAuth({ ENV }, API);
+  return augmentAPIWithCORS(await augmentAPIWithFakeAuth({ ENV }, API));
}

To see a real example have a look at the @whook/example.

Note that you can define individual CORS values on the handler definitions usins the x-whook property.

API

Functions

augmentAPIWithCORS(API) ⇒ Promise.<Object>

Augment an OpenAPI to also serve OPTIONS methods with the CORS added.

Kind: global function
Returns: Promise.<Object> - The augmented OpenAPI object

ParamTypeDescription
APIObjectThe OpenAPI object

wrapErrorHandlerForCORS(services) ⇒ Promise.<Object>

Wrap the error handler service as a last chance to add CORS

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services depended on
services.CORSObjectA CORS object to be added to errors responses
services.logObjectnoopAn optional logging service

initWrapHandlerWithCORS(services) ⇒ Promise.<Object>

Wrap an handler to append CORS to response.

Kind: global function
Returns: Promise.<Object> - A promise of an object containing the reshaped env vars.

ParamTypeDefaultDescription
servicesObjectThe services ENV depends on
services.CORSObjectA CORS object to be added to errors responses
services.logObjectnoopAn optional logging service

Authors

License

MIT

16.1.0

2 months ago

16.0.1

2 months ago

16.0.0

2 months ago

15.0.0

3 months ago

13.0.0

9 months ago

13.1.2

9 months ago

13.1.0

9 months ago

13.2.0

7 months ago

14.0.0

6 months ago

13.2.1

7 months ago

10.0.5

2 years ago

10.0.6

2 years ago

10.0.0

2 years ago

10.0.1

2 years ago

10.0.2

2 years ago

10.0.3

2 years ago

10.0.4

2 years ago

12.0.0

1 year ago

12.0.1

1 year ago

12.0.2

1 year ago

11.0.0

1 year ago

11.0.1

1 year ago

9.0.1

2 years ago

9.0.0

2 years ago

8.5.1

3 years ago

8.5.0

3 years ago

8.4.2

3 years ago

8.4.1

3 years ago

8.4.0

3 years ago

8.3.0

3 years ago

8.1.0

3 years ago

8.1.1

3 years ago

8.0.4

3 years ago

8.0.1

3 years ago

8.0.0

3 years ago

8.0.3

3 years ago

8.0.2

3 years ago

7.1.5

3 years ago

7.1.4

3 years ago

7.1.3

3 years ago

7.1.2

3 years ago

7.1.1

3 years ago

7.1.0

3 years ago

7.0.0

3 years ago

6.0.0

3 years ago

5.1.5

4 years ago

5.1.6

4 years ago

5.1.4

4 years ago

5.1.3

4 years ago

5.1.2

4 years ago

5.1.1

4 years ago

5.1.0

4 years ago

5.0.0

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.4

4 years ago

4.0.2

4 years ago

4.0.1

4 years ago

4.0.0

4 years ago

4.0.0-alpha.44

4 years ago

4.0.0-alpha.43

4 years ago

4.0.0-alpha.42

4 years ago

4.0.0-alpha.41

4 years ago

4.0.0-alpha.40

4 years ago

4.0.0-alpha.39

4 years ago

4.0.0-alpha.38

4 years ago

4.0.0-alpha.37

4 years ago

4.0.0-alpha.36

4 years ago

4.0.0-alpha.35

4 years ago

4.0.0-alpha.34

4 years ago

4.0.0-alpha.33

4 years ago

4.0.0-alpha.32

4 years ago

4.0.0-alpha.31

4 years ago

4.0.0-alpha.30

4 years ago

4.0.0-alpha.29

4 years ago

4.0.0-alpha.28

4 years ago

4.0.0-alpha.27

4 years ago

4.0.0-alpha.26

4 years ago

4.0.0-alpha.25

4 years ago

4.0.0-alpha.24

4 years ago

4.0.0-alpha.20

4 years ago

4.0.0-alpha.23

4 years ago

4.0.0-alpha.22

4 years ago

4.0.0-alpha.21

4 years ago

4.0.0-alpha.19

4 years ago

4.0.0-alpha.18

4 years ago

4.0.0-alpha.17

4 years ago

4.0.0-alpha.16

4 years ago

4.0.0-alpha.15

4 years ago

4.0.0-alpha.14

4 years ago

4.0.0-alpha.13

4 years ago

4.0.0-alpha.12

4 years ago

4.0.0-alpha.11

5 years ago

4.0.0-alpha.10

5 years ago

4.0.0-alpha.9

5 years ago

4.0.0-alpha.8

5 years ago

4.0.0-alpha.7

5 years ago

4.0.0-alpha.6

5 years ago

4.0.0-alpha.5

5 years ago

4.0.0-alpha.4

5 years ago

4.0.0-alpha.3

5 years ago

4.0.0-alpha.0

5 years ago