@whook/gcp-functions 8.5.1 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +51 -40
  2. package/dist/commands/testHTTPFunction.d.ts +1 -1
  3. package/dist/commands/testHTTPFunction.js +119 -150
  4. package/dist/commands/testHTTPFunction.js.map +1 -1
  5. package/dist/index.d.ts +3 -6
  6. package/dist/index.js +182 -269
  7. package/dist/index.js.map +1 -1
  8. package/dist/libs/utils.js +16 -35
  9. package/dist/libs/utils.js.map +1 -1
  10. package/dist/services/_autoload.d.ts +14 -2
  11. package/dist/services/_autoload.js +85 -105
  12. package/dist/services/_autoload.js.map +1 -1
  13. package/dist/services/log.d.ts +4 -1
  14. package/dist/services/log.js +2 -12
  15. package/dist/services/log.js.map +1 -1
  16. package/dist/services/log.test.js +4 -9
  17. package/dist/services/log.test.js.map +1 -1
  18. package/dist/wrappers/googleHTTPFunction.js +246 -292
  19. package/dist/wrappers/googleHTTPFunction.js.map +1 -1
  20. package/package.json +55 -98
  21. package/src/commands/testHTTPFunction.ts +12 -19
  22. package/src/index.ts +48 -62
  23. package/src/libs/utils.ts +4 -5
  24. package/src/services/_autoload.ts +127 -122
  25. package/src/services/log.test.ts +2 -2
  26. package/src/wrappers/googleHTTPFunction.ts +34 -32
  27. package/dist/commands/testHTTPFunction.mjs +0 -136
  28. package/dist/commands/testHTTPFunction.mjs.map +0 -1
  29. package/dist/index.mjs +0 -265
  30. package/dist/index.mjs.map +0 -1
  31. package/dist/libs/utils.mjs +0 -27
  32. package/dist/libs/utils.mjs.map +0 -1
  33. package/dist/services/_autoload.mjs +0 -107
  34. package/dist/services/_autoload.mjs.map +0 -1
  35. package/dist/services/log.mjs +0 -4
  36. package/dist/services/log.mjs.map +0 -1
  37. package/dist/services/log.test.mjs +0 -7
  38. package/dist/services/log.test.mjs.map +0 -1
  39. package/dist/wrappers/googleHTTPFunction.mjs +0 -288
  40. package/dist/wrappers/googleHTTPFunction.mjs.map +0 -1
package/README.md CHANGED
@@ -8,7 +8,6 @@
8
8
  > Build and deploy to GCP Cloud Functions with Whook.
9
9
 
10
10
  [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/nfroidure/whook/blob/master/packages/whook-gcp-functions/LICENSE)
11
- [![NPM version](https://badge.fury.io/js/%40whook%2Fgcp-functions.svg)](https://npmjs.org/package/@whook/gcp-functions)
12
11
 
13
12
 
14
13
  [//]: # (::contents:start)
@@ -22,18 +21,21 @@ You can find a complete setup with a Terraform deployment example in
22
21
 
23
22
  ## Quick setup
24
23
 
25
- Install this module and its peer dependencies :
24
+ Install this module:
26
25
 
27
26
  ```sh
28
27
  npm i @whook/gcp-functions;
29
- npm i --save-dev @whook/http-transaction esbuild
30
28
  ```
31
29
 
32
30
  Add this module to your Whook plugins and tweak the 2 build functions in your
33
- `build.ts` main file:
31
+ `src/build.ts` main file:
34
32
 
35
33
  ```diff
36
- - import YError from 'yerror';
34
+ import {
35
+ // (...)
36
+ - runBuild as runBaseBuild,
37
+ // (...)
38
+ } from '@whook/whook';
37
39
  +import {
38
40
  + runBuild as runBaseBuild,
39
41
  + prepareBuildEnvironment as prepareBaseBuildEnvironment,
@@ -61,20 +63,6 @@ export async function prepareEnvironment(
61
63
 
62
64
  // (...)
63
65
 
64
- // The `runBuild` function is intended to build the
65
- // project
66
- export async function runBuild(
67
- innerPrepareEnvironment = prepareBuildEnvironment,
68
- ): Promise<void> {
69
- - throw new YError('E_NO_BUILD_IMPLEMENTED');
70
-
71
- // Usually, here you call the installed build
72
- - // return runBaseBuild(innerPrepareEnvironment);
73
- + return runBaseBuild(innerPrepareEnvironment);
74
- }
75
-
76
- // (...)
77
-
78
66
  // The `prepareBuildEnvironment` create the build
79
67
  // environment
80
68
  export async function prepareBuildEnvironment(
@@ -109,48 +97,71 @@ export async function prepareBuildEnvironment(
109
97
  }
110
98
  ```
111
99
 
112
- And add the GCP Functions config (usually in `src/config/common/config.js`):
113
-
100
+ Declare this module types in your `src/whook.d.ts` type
101
+ definitions:
114
102
  ```diff
115
103
  + import type { WhookCompilerConfig } from '@whook/whook';
116
104
  + import type {
117
- + WhookAPIOperationGCPFunctionConfig
105
+ + WhookGCPBuildConfig,
106
+ + WhookAPIOperationGCPFunctionConfig
118
107
  + } from '@whook/gcp-functions';
119
108
 
120
- // ...
109
+ declare module '@whook/whook' {
110
+
111
+ // ...
121
112
 
122
- export type AppConfigs = WhookConfigs &
123
- + WhookCompilerConfig &
124
- APIConfig;
113
+ export interface WhookConfigs
114
+ - extends WhookBaseConfigs {}
115
+ + extends WhookBaseConfigs,
116
+ + WhookGCPBuildConfig,
117
+ + WhookCompilerConfig {}
125
118
 
126
- const CONFIG: AppConfigs = {
119
+ // ...
120
+
121
+ export interface WhookAPIHandlerDefinition<
122
+ T extends Record<string, unknown> = Record<string, unknown>,
123
+ U extends {
124
+ [K in keyof U]: K extends `x-${string}` ? Record<string, unknown> : never;
125
+ } = unknown,
126
+ V extends Record<string, unknown> = Record<string, unknown>,
127
+ > extends WhookBaseAPIHandlerDefinition<T, U> {
128
+ operation: U & WhookAPIOperation<
129
+ T &
130
+ + WhookAPIOperationGCPFunctionConfig &
131
+ WhookAPIOperationCORSConfig
132
+ >;
133
+ }
134
+
135
+ }
136
+ ```
137
+ And add the GCP Functions config (usually in `src/config/common/config.js`):
138
+
139
+ ```diff
140
+ import type { WhookConfigs } from '@whook/whook';
141
+
142
+ // ...
143
+
144
+ const CONFIG: WhookConfigs = {
127
145
  // ...
128
146
  + COMPILER_OPTIONS: {
129
147
  + externalModules: [],
130
- + ignoredModules: [],
131
- + target: '12', // Node 14 is in public review
148
+ + target: '16',
132
149
  + },
133
150
  };
134
151
 
135
- // Export custom handlers definitions
136
- export type APIHandlerDefinition = WhookAPIHandlerDefinition<
137
- + WhookAPIOperationGCPFunctionConfig &
138
- WhookAPIOperationSwaggerConfig
139
- >;
140
-
141
152
  export default CONFIG;
142
153
  ```
143
154
 
144
155
  # Build
145
156
 
146
- To build your functions :
157
+ To build your functions:
147
158
 
148
159
  ```sh
149
160
  # Build all functions
150
- npm run compile && npm run build
161
+ npm run build
151
162
 
152
163
  # Build only one function
153
- npm run compile && npm run build -- getPing
164
+ npm run build -- getPing
154
165
  ```
155
166
 
156
167
  # Debug
@@ -163,10 +174,10 @@ your `WHOOK_PLUGINS` list. It provides you some commands like the
163
174
  npx whook testHTTPFunction --name getPing
164
175
  ```
165
176
 
166
- To get more insights when errors happens:
177
+ To get more insights when some errors happens:
167
178
 
168
179
  ```sh
169
- npm run whook-dev -- testHTTPFunction --name getPing
180
+ DEBUG=whook npm run whook-dev -- testHTTPFunction --name getPing
170
181
  ```
171
182
 
172
183
  ## Deployment
@@ -7,6 +7,6 @@ declare const _default: import("knifecycle").ServiceInitializer<{
7
7
  PROJECT_DIR: string;
8
8
  API: OpenAPIV3.Document<{}>;
9
9
  log: LogService;
10
- args: WhookCommandArgs;
10
+ args: WhookCommandArgs<Record<string, import("@whook/cli").WhookArgsTypes>>;
11
11
  }, () => Promise<void>>;
12
12
  export default _default;
@@ -1,156 +1,125 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.definition = exports.default = void 0;
7
-
8
- var _utils = require("../libs/utils");
9
-
10
- var _knifecycle = require("knifecycle");
11
-
12
- var _cli = require("@whook/cli");
13
-
14
- var _yerror = _interopRequireDefault(require("yerror"));
15
-
16
- var _httpRouter = require("@whook/http-router");
17
-
18
- var _stream = _interopRequireDefault(require("stream"));
19
-
20
- var _camelcase = _interopRequireDefault(require("camelcase"));
21
-
22
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
1
+ import { loadLambda } from '../libs/utils.js';
2
+ import { extra, autoService } from 'knifecycle';
3
+ import { readArgs } from '@whook/cli';
4
+ import { YError } from 'yerror';
5
+ import { dereferenceOpenAPIOperations, getOpenAPIOperations, } from '@whook/http-router';
6
+ import stream from 'stream';
7
+ import camelCase from 'camelcase';
24
8
  const SEARCH_SEPARATOR = '?';
25
9
  const PATH_SEPARATOR = '/';
26
- const definition = {
27
- description: 'A command for testing AWS HTTP lambda',
28
- example: `whook testHTTPLambda --name getPing`,
29
- arguments: {
30
- type: 'object',
31
- additionalProperties: false,
32
- required: ['name'],
33
- properties: {
34
- name: {
35
- description: 'Name of the lamda to run',
36
- type: 'string'
37
- },
38
- type: {
39
- description: 'Type of lambda to test',
40
- type: 'string',
41
- enum: ['main', 'index'],
42
- default: 'index'
43
- },
44
- contentType: {
45
- description: 'Content type of the payload',
46
- type: 'string',
47
- default: 'application/json'
48
- },
49
- parameters: {
50
- description: 'The HTTP call parameters',
51
- type: 'string',
52
- default: '{}'
53
- }
54
- }
55
- }
10
+ export const definition = {
11
+ description: 'A command for testing AWS HTTP lambda',
12
+ example: `whook testHTTPLambda --name getPing`,
13
+ arguments: {
14
+ type: 'object',
15
+ additionalProperties: false,
16
+ required: ['name'],
17
+ properties: {
18
+ name: {
19
+ description: 'Name of the lamda to run',
20
+ type: 'string',
21
+ },
22
+ type: {
23
+ description: 'Type of lambda to test',
24
+ type: 'string',
25
+ enum: ['main', 'index'],
26
+ default: 'index',
27
+ },
28
+ contentType: {
29
+ description: 'Content type of the payload',
30
+ type: 'string',
31
+ default: 'application/json',
32
+ },
33
+ parameters: {
34
+ description: 'The HTTP call parameters',
35
+ type: 'string',
36
+ default: '{}',
37
+ },
38
+ },
39
+ },
56
40
  };
57
- exports.definition = definition;
58
-
59
- var _default = (0, _knifecycle.extra)(definition, (0, _knifecycle.service)(initTestHTTPLambdaCommand, "testHTTPLambdaCommand", ["NODE_ENV", "PROJECT_DIR", "API", "log", "args"]));
60
-
61
- exports.default = _default;
62
-
63
- async function initTestHTTPLambdaCommand({
64
- NODE_ENV,
65
- PROJECT_DIR,
66
- API,
67
- log,
68
- args
69
- }) {
70
- return async () => {
71
- const {
72
- name,
73
- type,
74
- contentType,
75
- parameters: rawParameters
76
- } = (0, _cli.readArgs)(definition.arguments, args);
77
- const handler = await (0, _utils.loadLambda)({
78
- PROJECT_DIR,
79
- log
80
- }, NODE_ENV, name, type);
81
- const OPERATION = (await (0, _httpRouter.dereferenceOpenAPIOperations)(API, (0, _httpRouter.getOpenAPIOperations)(API))).find(({
82
- operationId
83
- }) => operationId === name);
84
-
85
- if (!OPERATION) {
86
- throw new _yerror.default('E_OPERATION_NOT_FOUND');
87
- }
88
-
89
- const hasBody = !!OPERATION.requestBody;
90
- const parameters = JSON.parse(rawParameters);
91
- const search = (OPERATION.parameters || []).filter(p => p.in === 'query').reduce((accSearch, p) => {
92
- if (null != parameters[p.name]) {
93
- return accSearch + (accSearch ? '&' : '') + p.name + '=' + parameters[p.name];
94
- }
95
-
96
- return accSearch;
97
- }, '');
98
- const path = OPERATION.path.split(PATH_SEPARATOR).map(part => {
99
- const matches = /^\{([\d\w]+)\}$/i.exec(part);
100
-
101
- if (matches) {
102
- return parameters[matches[1]];
103
- }
104
-
105
- return part;
106
- }).join(PATH_SEPARATOR);
107
- const gcpfRequest = {
108
- method: OPERATION.method,
109
- originalUrl: path + (search ? SEARCH_SEPARATOR + search : ''),
110
- headers: (OPERATION.parameters || []).filter(p => p.in === 'header').reduce((headerParameters, p) => {
111
- headerParameters[p.name] = parameters[(0, _camelcase.default)(p.name)];
112
- return headerParameters;
113
- }, {}),
114
- rawBody: Buffer.from(hasBody ? contentType === 'application/json' ? parameters.body ? JSON.stringify(parameters.body) : '' : parameters.body || '' : '')
115
- };
116
-
117
- if (hasBody) {
118
- gcpfRequest.headers['content-type'] = `${contentType};charset=UTF-8`;
119
- }
120
-
121
- log('info', 'GCPF_REQUEST:', gcpfRequest);
122
- const response = {
123
- status: 0,
124
- headers: {},
125
- data: ''
126
- };
127
- await new Promise((resolve, reject) => {
128
- const gcpfResponse = new _stream.default.PassThrough();
129
-
130
- gcpfResponse.set = (name, value) => {
131
- response.headers[name] = value;
132
- };
133
-
134
- gcpfResponse.status = code => {
135
- response.status = code;
136
- };
137
-
138
- handler(gcpfRequest, gcpfResponse).catch(reject);
139
- const chunks = [];
140
- gcpfResponse.once('end', () => {
141
- response.data = Buffer.concat(chunks).toString();
142
- resolve();
143
- });
144
- gcpfResponse.once('error', reject);
145
- gcpfResponse.on('readable', () => {
146
- let data;
147
-
148
- while (data = gcpfResponse.read()) {
149
- chunks.push(data);
41
+ export default extra(definition, autoService(initTestHTTPLambdaCommand));
42
+ async function initTestHTTPLambdaCommand({ NODE_ENV, PROJECT_DIR, API, log, args, }) {
43
+ return async () => {
44
+ const { namedArguments: { name, type, contentType, parameters: rawParameters }, } = readArgs(definition.arguments, args);
45
+ const handler = await loadLambda({ PROJECT_DIR, log }, NODE_ENV, name, type);
46
+ const OPERATION = (await dereferenceOpenAPIOperations(API, getOpenAPIOperations(API))).find(({ operationId }) => operationId === name);
47
+ if (!OPERATION) {
48
+ throw new YError('E_OPERATION_NOT_FOUND');
150
49
  }
151
- });
152
- });
153
- log('info', 'SUCCESS:', response);
154
- };
50
+ const hasBody = !!OPERATION.requestBody;
51
+ const parameters = JSON.parse(rawParameters);
52
+ const search = (OPERATION.parameters || [])
53
+ .filter((p) => p.in === 'query')
54
+ .reduce((accSearch, p) => {
55
+ if (null != parameters[p.name]) {
56
+ return (accSearch +
57
+ (accSearch ? '&' : '') +
58
+ p.name +
59
+ '=' +
60
+ parameters[p.name]);
61
+ }
62
+ return accSearch;
63
+ }, '');
64
+ const path = OPERATION.path
65
+ .split(PATH_SEPARATOR)
66
+ .map((part) => {
67
+ const matches = /^\{([\d\w]+)\}$/i.exec(part);
68
+ if (matches) {
69
+ return parameters[matches[1]];
70
+ }
71
+ return part;
72
+ })
73
+ .join(PATH_SEPARATOR);
74
+ const gcpfRequest = {
75
+ method: OPERATION.method,
76
+ originalUrl: path + (search ? SEARCH_SEPARATOR + search : ''),
77
+ headers: (OPERATION.parameters || [])
78
+ .filter((p) => p.in === 'header')
79
+ .reduce((headerParameters, p) => {
80
+ headerParameters[p.name] = parameters[camelCase(p.name)];
81
+ return headerParameters;
82
+ }, {}),
83
+ rawBody: Buffer.from(hasBody
84
+ ? contentType === 'application/json'
85
+ ? parameters.body
86
+ ? JSON.stringify(parameters.body)
87
+ : ''
88
+ : parameters.body || ''
89
+ : ''),
90
+ };
91
+ if (hasBody) {
92
+ gcpfRequest.headers['content-type'] = `${contentType};charset=UTF-8`;
93
+ }
94
+ log('info', 'GCPF_REQUEST:', gcpfRequest);
95
+ const response = {
96
+ status: 0,
97
+ headers: {},
98
+ data: '',
99
+ };
100
+ await new Promise((resolve, reject) => {
101
+ const gcpfResponse = new stream.PassThrough();
102
+ gcpfResponse.set = (name, value) => {
103
+ response.headers[name] = value;
104
+ };
105
+ gcpfResponse.status = (code) => {
106
+ response.status = code;
107
+ };
108
+ handler(gcpfRequest, gcpfResponse).catch(reject);
109
+ const chunks = [];
110
+ gcpfResponse.once('end', () => {
111
+ response.data = Buffer.concat(chunks).toString();
112
+ resolve();
113
+ });
114
+ gcpfResponse.once('error', reject);
115
+ gcpfResponse.on('readable', () => {
116
+ let data;
117
+ while ((data = gcpfResponse.read())) {
118
+ chunks.push(data);
119
+ }
120
+ });
121
+ });
122
+ log('info', 'SUCCESS:', response);
123
+ };
155
124
  }
156
125
  //# sourceMappingURL=testHTTPFunction.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/commands/testHTTPFunction.ts"],"names":["SEARCH_SEPARATOR","PATH_SEPARATOR","definition","description","example","arguments","type","additionalProperties","required","properties","name","enum","default","contentType","parameters","initTestHTTPLambdaCommand","NODE_ENV","PROJECT_DIR","API","log","args","rawParameters","handler","OPERATION","find","operationId","YError","hasBody","requestBody","JSON","parse","search","filter","p","in","reduce","accSearch","path","split","map","part","matches","exec","join","gcpfRequest","method","originalUrl","headers","headerParameters","rawBody","Buffer","from","body","stringify","response","status","data","Promise","resolve","reject","gcpfResponse","stream","PassThrough","set","value","code","catch","chunks","once","concat","toString","on","read","push"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA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,CAACC,OAAD,EAAUC,MAAV,KAAqB;AAC3C,YAAMC,YAAiB,GAAG,IAAIC,gBAAOC,WAAX,EAA1B;;AAEAF,MAAAA,YAAY,CAACG,GAAb,GAAmB,CAACrD,IAAD,EAAesD,KAAf,KAAiC;AAClDV,QAAAA,QAAQ,CAACP,OAAT,CAAiBrC,IAAjB,IAAyBsD,KAAzB;AACD,OAFD;;AAGAJ,MAAAA,YAAY,CAACL,MAAb,GAAuBU,IAAD,IAAkB;AACtCX,QAAAA,QAAQ,CAACC,MAAT,GAAkBU,IAAlB;AACD,OAFD;;AAIA3C,MAAAA,OAAO,CAACsB,WAAD,EAAcgB,YAAd,CAAP,CAAmCM,KAAnC,CAAyCP,MAAzC;AAEA,YAAMQ,MAAM,GAAG,EAAf;AAEAP,MAAAA,YAAY,CAACQ,IAAb,CAAkB,KAAlB,EAAyB,MAAM;AAC7Bd,QAAAA,QAAQ,CAACE,IAAT,GAAgBN,MAAM,CAACmB,MAAP,CAAcF,MAAd,EAAsBG,QAAtB,EAAhB;AACAZ,QAAAA,OAAO;AACR,OAHD;AAIAE,MAAAA,YAAY,CAACQ,IAAb,CAAkB,OAAlB,EAA2BT,MAA3B;AACAC,MAAAA,YAAY,CAACW,EAAb,CAAgB,UAAhB,EAA4B,MAAM;AAChC,YAAIf,IAAJ;;AACA,eAAQA,IAAI,GAAGI,YAAY,CAACY,IAAb,EAAf,EAAqC;AACnCL,UAAAA,MAAM,CAACM,IAAP,CAAYjB,IAAZ;AACD;AACF,OALD;AAMD,KAzBK,CAAN;AA0BArC,IAAAA,GAAG,CAAC,MAAD,EAAS,UAAT,EAAqBmC,QAArB,CAAH;AACD,GA/GD;AAgHD","sourcesContent":["import { loadLambda } from '../libs/utils';\nimport { extra, autoService } from 'knifecycle';\nimport { readArgs } from '@whook/cli';\nimport YError from 'yerror';\nimport {\n dereferenceOpenAPIOperations,\n getOpenAPIOperations,\n} from '@whook/http-router';\nimport stream from 'stream';\nimport camelCase from 'camelcase';\nimport type {\n WhookCommandArgs,\n WhookCommandDefinition,\n WhookCommandNamedArgs,\n} from '@whook/cli';\nimport type { LogService } from 'common-services';\nimport type { OpenAPIV3 } from 'openapi-types';\n\nconst SEARCH_SEPARATOR = '?';\nconst PATH_SEPARATOR = '/';\n\nexport const definition: WhookCommandDefinition = {\n description: 'A command for testing AWS HTTP lambda',\n example: `whook testHTTPLambda --name getPing`,\n arguments: {\n type: 'object',\n additionalProperties: false,\n required: ['name'],\n properties: {\n name: {\n description: 'Name of the lamda to run',\n type: 'string',\n },\n type: {\n description: 'Type of lambda to test',\n type: 'string',\n enum: ['main', 'index'],\n default: 'index',\n },\n contentType: {\n description: 'Content type of the payload',\n type: 'string',\n default: 'application/json',\n },\n parameters: {\n description: 'The HTTP call parameters',\n type: 'string',\n default: '{}',\n },\n },\n },\n};\n\nexport default extra(definition, autoService(initTestHTTPLambdaCommand));\n\nasync function initTestHTTPLambdaCommand({\n NODE_ENV,\n PROJECT_DIR,\n API,\n log,\n args,\n}: {\n NODE_ENV: string;\n PROJECT_DIR: string;\n API: OpenAPIV3.Document;\n log: LogService;\n args: WhookCommandArgs;\n}) {\n return async () => {\n const {\n name,\n type,\n contentType,\n parameters: rawParameters,\n }: WhookCommandNamedArgs = readArgs(definition.arguments, args) as {\n name: string;\n type: string;\n contentType: string;\n parameters: string;\n };\n const handler = await loadLambda(\n { PROJECT_DIR, log },\n NODE_ENV,\n name,\n type,\n );\n const OPERATION = (\n await dereferenceOpenAPIOperations(API, getOpenAPIOperations(API))\n ).find(({ operationId }) => operationId === name);\n\n if (!OPERATION) {\n throw new YError('E_OPERATION_NOT_FOUND');\n }\n\n const hasBody = !!OPERATION.requestBody;\n const parameters = JSON.parse(rawParameters);\n const search = ((OPERATION.parameters || []) as OpenAPIV3.ParameterObject[])\n .filter((p) => p.in === 'query')\n .reduce((accSearch, p) => {\n if (null != parameters[p.name]) {\n return (\n accSearch +\n (accSearch ? '&' : '') +\n p.name +\n '=' +\n parameters[p.name]\n );\n }\n return accSearch;\n }, '');\n\n const path = OPERATION.path\n .split(PATH_SEPARATOR)\n\n .map((part) => {\n const matches = /^\\{([\\d\\w]+)\\}$/i.exec(part);\n\n if (matches) {\n return parameters[matches[1]];\n }\n return part;\n })\n .join(PATH_SEPARATOR);\n const gcpfRequest = {\n method: OPERATION.method,\n originalUrl: path + (search ? SEARCH_SEPARATOR + search : ''),\n headers: ((OPERATION.parameters || []) as OpenAPIV3.ParameterObject[])\n .filter((p) => p.in === 'header')\n .reduce((headerParameters, p) => {\n headerParameters[p.name] = parameters[camelCase(p.name)];\n return headerParameters;\n }, {}),\n rawBody: Buffer.from(\n hasBody\n ? contentType === 'application/json'\n ? parameters.body\n ? JSON.stringify(parameters.body)\n : ''\n : parameters.body || ''\n : '',\n ),\n };\n if (hasBody) {\n gcpfRequest.headers['content-type'] = `${contentType};charset=UTF-8`;\n }\n log('info', 'GCPF_REQUEST:', gcpfRequest);\n\n const response = {\n status: 0,\n headers: {},\n data: '',\n };\n await new Promise<void>((resolve, reject) => {\n const gcpfResponse: any = new stream.PassThrough();\n\n gcpfResponse.set = (name: string, value: string) => {\n response.headers[name] = value;\n };\n gcpfResponse.status = (code: number) => {\n response.status = code;\n };\n\n handler(gcpfRequest, gcpfResponse).catch(reject);\n\n const chunks = [];\n\n gcpfResponse.once('end', () => {\n response.data = Buffer.concat(chunks).toString();\n resolve();\n });\n gcpfResponse.once('error', reject);\n gcpfResponse.on('readable', () => {\n let data;\n while ((data = gcpfResponse.read())) {\n chunks.push(data);\n }\n });\n });\n log('info', 'SUCCESS:', response);\n };\n}\n"],"file":"testHTTPFunction.js"}
1
+ {"version":3,"file":"testHTTPFunction.js","sourceRoot":"","sources":["../../src/commands/testHTTPFunction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,QAAQ,EAAE,MAAM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C;AACJ,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,10 +1,7 @@
1
- import Knifecycle from 'knifecycle';
2
- import type { Dependencies } from 'knifecycle';
1
+ import { Knifecycle } from 'knifecycle';
3
2
  import type { WhookCompilerOptions } from '@whook/whook';
4
- import type { BuildOptions } from 'knifecycle/dist/build';
5
3
  export declare const DEFAULT_BUILD_PARALLELISM = 10;
6
- export declare type WhookBuildConfig = {
7
- BUILD_OPTIONS?: BuildOptions;
4
+ export declare type WhookGCPBuildConfig = {
8
5
  BUILD_PARALLELISM?: number;
9
6
  };
10
7
  export declare type WhookAPIOperationGCPFunctionConfig = {
@@ -14,5 +11,5 @@ export declare type WhookAPIOperationGCPFunctionConfig = {
14
11
  compilerOptions?: WhookCompilerOptions;
15
12
  suffix?: string;
16
13
  };
17
- export declare function prepareBuildEnvironment<T extends Knifecycle<Dependencies>>($?: T): Promise<T>;
14
+ export declare function prepareBuildEnvironment<T extends Knifecycle>($?: T): Promise<T>;
18
15
  export declare function runBuild(aPrepareBuildEnvironment: typeof prepareBuildEnvironment): Promise<void>;