wirejs-deploy-amplify-basic 0.0.12-alpha → 0.0.13-alpha

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.
@@ -1,22 +1,75 @@
1
1
  import { env } from 'process';
2
+ import { LambdaFunctionURLHandler, APIGatewayProxyEventV2 } from 'aws-lambda';
3
+ import { CookieJar, Context, requiresContext } from 'wirejs-resources';
4
+ import * as api from '../../../api/index';
2
5
 
3
- import { LambdaFunctionURLHandler } from 'aws-lambda';
6
+ function createContext(event: APIGatewayProxyEventV2) {
7
+ const baseUrl = `https://${event.requestContext.domainName}${event.rawPath}`;
8
+ const queryString = event.queryStringParameters
9
+ ? `?${new URLSearchParams(event.queryStringParameters as any).toString()}`
10
+ : '';
11
+ const location = new URL(`${baseUrl}${queryString}`);
4
12
 
5
- // import {
6
- // S3Client,
7
- // ListObjectsCommand,
8
- // PutObjectCommand,
9
- // GetObjectCommand
10
- // } from '@aws-sdk/client-s3';
13
+ const cookies = new CookieJar(event.headers['cookie']);
14
+ return new Context({ cookies, location });
15
+ }
11
16
 
12
- // import * as api from '../src/api/index.js';
13
-
14
- // const s3 = new S3Client()
17
+ async function callApiMethod(api: any, call: any, context: any) {
18
+ try {
19
+ const [scope, ...rest] = call.method;
20
+ console.log('api method parsed', { scope, rest });
21
+ if (rest.length === 0) {
22
+ console.log('api method resolved. invoking...');
23
+ if (requiresContext(api[scope])) {
24
+ return {
25
+ data: await (api[scope] as any)(context, ...call.args.slice(1))
26
+ };
27
+ } else {
28
+ return {
29
+ data: await api[scope](...call.args)
30
+ };
31
+ }
32
+ } else {
33
+ console.log('nested scope found');
34
+ return callApiMethod(api[scope], {
35
+ ...call,
36
+ method: rest,
37
+ }, context);
38
+ }
39
+ } catch (error: any) {
40
+ console.log(error);
41
+ return { error: error.message };
42
+ }
43
+ }
15
44
 
16
45
  export const handler: LambdaFunctionURLHandler = async (event, context) => {
46
+ const calls = JSON.parse(event.body!);
47
+ const responses = [];
48
+
49
+ for (const call of calls) {
50
+ console.log('handling API call', call);
51
+ responses.push(await callApiMethod(api, call, context));
52
+ }
53
+
54
+ const wjsContext = createContext(event);
55
+
56
+ console.log('setting cookies', wjsContext.cookies.getSetCookies());
57
+
58
+ const cookies: string[] = [];
59
+ for (const cookie of wjsContext.cookies.getSetCookies()) {
60
+ const cookieOptions = [];
61
+ if (cookie.maxAge) cookieOptions.push(`Max-Age=${cookie.maxAge}`);
62
+ if (cookie.httpOnly) cookieOptions.push('HttpOnly');
63
+ if (cookie.secure) cookieOptions.push('Secure');
64
+ cookies.push(`${cookie.name}=${cookie.value}; ${cookieOptions.join('; ')}`);
65
+ }
66
+
17
67
  return {
18
68
  statusCode: 200,
19
- cookies: [],
20
- body: 'Hello.'
69
+ cookies,
70
+ headers: {
71
+ 'Content-Type': 'application/json; charset=utf-8'
72
+ },
73
+ body: JSON.stringify(responses)
21
74
  }
22
75
  }
@@ -3,6 +3,6 @@
3
3
  "dependencies": {
4
4
  "jsdom": "^25.0.0",
5
5
  "wirejs-dom": "^1.0.34",
6
- "wirejs-resources": "^0.1.5-alpha"
6
+ "wirejs-resources": "^0.1.6-alpha"
7
7
  }
8
8
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-deploy-amplify-basic",
3
- "version": "0.0.12-alpha",
3
+ "version": "0.0.13-alpha",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "wirejs-deploy-amplify-basic": "./build.js"
@@ -1,3 +1,10 @@
1
+ import {
2
+ S3Client,
3
+ ListObjectsCommand,
4
+ PutObjectCommand,
5
+ GetObjectCommand
6
+ } from '@aws-sdk/client-s3';
7
+
1
8
  import { overrides } from 'wirejs-resources';
2
9
  import { Resource } from 'wirejs-resources';
3
10