@justworkflowit/cdk-constructs 0.0.38 → 0.0.40

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.
@@ -0,0 +1,4 @@
1
+ import { JustWorkflowIt } from '@justworkflowit/api-client';
2
+ import { AssertiveClient } from '@smithy/types';
3
+ export declare const getErrorMessage: (err: unknown) => string;
4
+ export declare const getApiClient: () => AssertiveClient<JustWorkflowIt>;
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getApiClient = exports.getErrorMessage = void 0;
4
+ /* eslint-disable @typescript-eslint/no-explicit-any */
5
+ const api_client_1 = require("@justworkflowit/api-client");
6
+ const justWorkflowItApiExceptions_1 = require("./justWorkflowItApiExceptions");
7
+ const endpoint = process.env.API_BASE_URL;
8
+ const getAccessToken = async () => {
9
+ return process.env.AUTH_SECRET_NAME;
10
+ };
11
+ const cognitoIdentityProviderFactory = (_config) => {
12
+ const identityProvider = async (_props) => {
13
+ const token = await getAccessToken();
14
+ return {
15
+ id: 'cognito-user',
16
+ token,
17
+ };
18
+ };
19
+ return identityProvider;
20
+ };
21
+ const cognitoBearerSigner = {
22
+ sign: async (request) => {
23
+ const token = await getAccessToken();
24
+ request.headers['Authorization'] = `Bearer ${token}`;
25
+ return request;
26
+ },
27
+ };
28
+ const noAuthIdentityProviderFactory = (_config) => {
29
+ const identityProvider = (_props) => {
30
+ return Promise.resolve({
31
+ id: 'anonymous',
32
+ });
33
+ };
34
+ return identityProvider;
35
+ };
36
+ const noAuthSigner = {
37
+ sign: (request) => {
38
+ return Promise.resolve(request);
39
+ },
40
+ };
41
+ const getErrorMessage = (err) => {
42
+ const e = err;
43
+ const errorType = e?.errorType;
44
+ const message = e?.message;
45
+ const httpStatus = e?.$metadata?.httpStatusCode;
46
+ if (errorType === 'ValidationError' && e.fields) {
47
+ const fieldErrors = Object.entries(e.fields)
48
+ .map(([field, msg]) => `${field}: ${msg}`)
49
+ .join(', ');
50
+ return `Validation Error: ${fieldErrors}`;
51
+ }
52
+ if (errorType && message)
53
+ return `${errorType}: ${message}`;
54
+ if (message)
55
+ return message;
56
+ if (httpStatus)
57
+ return `Unexpected error (${httpStatus})`;
58
+ return 'Unexpected error';
59
+ };
60
+ exports.getErrorMessage = getErrorMessage;
61
+ const getApiClient = () => {
62
+ const client = new api_client_1.JustWorkflowIt({
63
+ endpoint,
64
+ httpAuthSchemes: [
65
+ {
66
+ schemeId: 'aws.auth#cognitoUserPools',
67
+ identityProvider: cognitoIdentityProviderFactory,
68
+ signer: cognitoBearerSigner,
69
+ },
70
+ {
71
+ schemeId: 'smithy.api#noAuth',
72
+ identityProvider: noAuthIdentityProviderFactory,
73
+ signer: noAuthSigner,
74
+ },
75
+ ],
76
+ });
77
+ const proxy = new Proxy(client, {
78
+ get(target, prop) {
79
+ const orig = target[prop];
80
+ if (typeof orig !== 'function')
81
+ return orig;
82
+ return async (...args) => {
83
+ try {
84
+ return await orig.apply(target, args);
85
+ }
86
+ catch (err) {
87
+ const rehydrated = await (0, justWorkflowItApiExceptions_1.deserializeSmithyError)(err);
88
+ throw rehydrated;
89
+ }
90
+ };
91
+ },
92
+ });
93
+ return proxy;
94
+ };
95
+ exports.getApiClient = getApiClient;
@@ -0,0 +1,3 @@
1
+ export type SmithyErrorClass = new (args: any) => Error;
2
+ export declare function buildSmithyErrorRegistry(): Promise<Record<string, SmithyErrorClass>>;
3
+ export declare function deserializeSmithyError(err: any): Promise<Error>;
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.buildSmithyErrorRegistry = buildSmithyErrorRegistry;
37
+ exports.deserializeSmithyError = deserializeSmithyError;
38
+ async function buildSmithyErrorRegistry() {
39
+ const module = await Promise.resolve().then(() => __importStar(require('@justworkflowit/api-client')));
40
+ console.log(module);
41
+ return Object.entries(module)
42
+ .filter(([key, val]) => {
43
+ return (typeof val === 'function' &&
44
+ key.endsWith('Error') // &&
45
+ // typeof (val as any).prototype?.errorType === 'string' &&
46
+ // typeof (val as any).prototype?.constructor === 'function'
47
+ );
48
+ })
49
+ .reduce((acc, [name, ctor]) => {
50
+ acc[name] = ctor;
51
+ return acc;
52
+ }, {});
53
+ }
54
+ async function deserializeSmithyError(err) {
55
+ if (!err || typeof err !== 'object')
56
+ return new Error('Unknown error');
57
+ const registry = await buildSmithyErrorRegistry();
58
+ const typeName = err?.errorType;
59
+ if (typeName && registry[typeName]) {
60
+ const ErrorClass = registry[typeName];
61
+ const { message, errorType, statusCode, ...rest } = err;
62
+ return new ErrorClass({
63
+ message: typeof message === 'string' ? message : 'Unknown error',
64
+ errorType,
65
+ statusCode,
66
+ ...rest,
67
+ });
68
+ }
69
+ return new Error(err?.message || 'Unknown error');
70
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@justworkflowit/cdk-constructs",
3
3
  "description": "",
4
- "version": "0.0.38",
4
+ "version": "0.0.40",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "publishConfig": {