16.1.0 • Published 2 months ago

@whook/gcp-functions v16.1.0

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

@whook/gcp-functions

Build and deploy to GCP Cloud Functions with Whook.

GitHub license

This module is aimed to help you to build and deploy your Whook server to Google Cloud Functions.

You can find a complete setup with a Terraform deployment example in this pull request.

Quick setup

Install this module:

npm i @whook/gcp-functions;

Add the plugin to the src/index.ts main file:

  // ...

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

  // ...

Tweak the 2 build functions in your src/build.ts main file:

import {
  // (...)
-  DEFAULT_INITIALIZER_PATH_MAP,
-  runBuild as runBaseBuild,
-  prepareBuildEnvironment as prepareBaseBuildEnvironment,
  // (...)
} from '@whook/whook';
+import {
+  DEFAULT_BUILD_INITIALIZER_PATH_MAP,
+  runBuild as runBaseBuild,
+  prepareBuildEnvironment as prepareBaseBuildEnvironment,
+} from '@whook/gcp-functions';

// (...)

// The `prepareBuildEnvironment` create the build
//  environment
export async function prepareBuildEnvironment(
  $: Knifecycle = new Knifecycle(),
): Promise<Knifecycle> {
  $ = await prepareEnvironment($);

  // (...)

-  // Usually, here you call the installed build env
-  // $ = await prepareBaseBuildEnvironment($);
+  // Calling the GCP specific build env
+  $ = await prepareBaseBuildEnvironment($);

  // The build often need to know were initializers
  //  can be found to create a static build and
  //  remove the need to create an injector
  $.register(
    constant('INITIALIZER_PATH_MAP', {
      // (...)
-      ...DEFAULT_INITIALIZER_PATH_MAP,
+      ...DEFAULT_BUILD_INITIALIZER_PATH_MAP,
    }),
  );

  // (...)

}

Declare this module types in your src/whook.d.ts type definitions:

// ...
+ import type { WhookCompilerConfig } from '@whook/whook';
+ import type {
+    WhookGCPBuildConfig,
+    WhookAPIOperationGCPFunctionConfig
+ } from '@whook/gcp-functions';

declare module 'application-services' {

  // ...

  export interface AppConfig
-    extends WhookBaseConfigs {}
+    extends WhookBaseConfigs,
+      WhookGCPBuildConfig,
+      WhookCompilerConfig {}

  // ...
}

// ...

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 &
+      WhookAPIOperationGCPFunctionConfig &
      WhookAPIOperationCORSConfig
    >;
  }
}

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

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

// ...

const CONFIG: AppConfig = {
  // ...
+  COMPILER_OPTIONS: {
+    externalModules: [],
+    target: '20',
+  },
};

export default CONFIG;

Build

To build your functions:

# Build all functions
npm run build

# Build only one function
npm run build -- getPing

Debug

You can easily test your function builds by adding @whook/gcp-functions to your WHOOK_PLUGINS list. It provides you some commands like the testHTTPFunction one:

npx whook testHTTPFunction --name getPing

To get more insights when some errors happens:

DEBUG=whook npm run whook-dev -- testHTTPFunction --name getPing

Deployment

We recommend using Terraform to deploy your functions.

There is a complete example on how to deploy your functions in this pull request.

API

Functions

initHandler(services) ⇒ Promise.<function()>

Initialize one Whook handler

Kind: global function
Returns: Promise.<function()> - A promise of the HANDLERS hash.

ParamTypeDefaultDescription
servicesObjectThe services $autoload depends on
services.WRAPPERSArrayAn optional list of wrappers to inject
services.logObjectnoopAn optional logging service
services.HANDLERSObjectThe rest is a hash of handlers mapped by their operation id

initWrapHandlerForGoogleHTTPFunction(services) ⇒ Promise.<Object>

Wrap an handler to make it work with GCP Functions.

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

ParamTypeDefaultDescription
servicesObjectThe services the wrapper depends on
services.OPERATION_APIObjectAn OpenAPI definitition for that handler
services.ENVObjectThe process environment
services.DEBUG_NODE_ENVSObjectThe NODE_ENV values that trigger debugging
services.DECODERSObjectRequest body decoders available
services.ENCODERSObjectResponse body encoders available
services.PARSERSObjectRequest body parsers available
services.STRINGIFYERSObjectResponse body stringifyers available
services.BUFFER_LIMITObjectThe buffer size limit
services.QUERY_PARSERObjectThe query parser to use
services.obfuscatorObjectA service to hide sensible values
services.errorHandlerObjectA service that changes any error to Whook response
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.1.2

9 months ago

13.1.0

9 months ago

13.2.2

8 months ago

13.2.0

8 months ago

14.0.0

6 months ago

13.2.1

8 months ago

13.0.0

9 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

2 years 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.3

3 years ago

8.0.1

3 years ago

8.0.2

3 years ago

8.0.0

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.5

4 years ago

4.0.5-alpha.0

4 years ago

4.0.4

4 years ago

4.0.3

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

3.1.3

4 years ago