@middy/s3 4.6.5 → 5.0.0-alpha.1

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 (4) hide show
  1. package/index.d.ts +30 -10
  2. package/index.js +74 -58
  3. package/package.json +5 -11
  4. package/index.cjs +0 -71
package/index.d.ts CHANGED
@@ -3,23 +3,43 @@ import { Options as MiddyOptions } from '@middy/util'
3
3
  import { Context as LambdaContext } from 'aws-lambda'
4
4
  import { GetObjectCommandInput, S3Client, S3ClientConfig } from '@aws-sdk/client-s3'
5
5
 
6
- export type Options<AwsS3Client = S3Client> = Omit<MiddyOptions<AwsS3Client, S3ClientConfig>, 'fetchData'>
6
+ export type GetObjectCommandInputNoChecksumMode = Omit<GetObjectCommandInput, 'ChecksumMode'> & {
7
+ ChecksumMode?: never
8
+ }
9
+ export type ParamType<T> = GetObjectCommandInputNoChecksumMode & { __returnType?: T }
10
+ export declare function s3Req<T> (req: GetObjectCommandInputNoChecksumMode): ParamType<T>
11
+
12
+ export type S3Options<AwsS3Client = S3Client> = Omit<MiddyOptions<AwsS3Client, S3ClientConfig>, 'fetchData'>
7
13
  & {
8
14
  fetchData?: {
9
- [key: string]: Omit<GetObjectCommandInput, 'ChecksumMode'> & {
10
- ChecksumMode?: never
11
- }
15
+ [key: string]: GetObjectCommandInputNoChecksumMode | ParamType<unknown>
12
16
  }
13
17
  }
14
18
 
15
- export type Context<TOptions extends Options | undefined> = TOptions extends {
16
- setToContext: true
17
- }
18
- ? LambdaContext & Record<keyof TOptions['fetchData'], any>
19
+ export type Context<TOptions extends S3Options | undefined> =
20
+ TOptions extends { setToContext: true }
21
+ ? TOptions extends { fetchData: infer TFetchData }
22
+ ? LambdaContext & {
23
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
24
+ ? T
25
+ : unknown
26
+ }
27
+ : never
19
28
  : LambdaContext
20
29
 
21
- declare function s3Middleware<TOptions extends Options | undefined> (
30
+ export type Internal<TOptions extends S3Options | undefined> =
31
+ TOptions extends S3Options
32
+ ? TOptions extends { fetchData: infer TFetchData }
33
+ ? {
34
+ [Key in keyof TFetchData]: TFetchData[Key] extends ParamType<infer T>
35
+ ? T
36
+ : unknown
37
+ }
38
+ : {}
39
+ : {}
40
+
41
+ declare function s3Middleware<TOptions extends S3Options | undefined> (
22
42
  options?: TOptions
23
- ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>>
43
+ ): middy.MiddlewareObj<unknown, any, Error, Context<TOptions>, Internal<TOptions>>
24
44
 
25
45
  export default s3Middleware
package/index.js CHANGED
@@ -1,61 +1,77 @@
1
- import { canPrefetch, createPrefetchClient, createClient, getCache, getInternal, processCache, modifyCache, jsonSafeParse } from '@middy/util';
2
- import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3';
1
+ import {
2
+ canPrefetch,
3
+ createPrefetchClient,
4
+ createClient,
5
+ getCache,
6
+ getInternal,
7
+ processCache,
8
+ modifyCache,
9
+ jsonSafeParse
10
+ } from '@middy/util'
11
+ import { S3Client, GetObjectCommand } from '@aws-sdk/client-s3'
3
12
  const defaults = {
4
- AwsClient: S3Client,
5
- awsClientOptions: {},
6
- awsClientAssumeRole: undefined,
7
- awsClientCapture: undefined,
8
- fetchData: {},
9
- disablePrefetch: false,
10
- cacheKey: 's3',
11
- cacheKeyExpiry: {},
12
- cacheExpiry: -1,
13
- setToContext: false
14
- };
15
- const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
16
- const s3Middleware = (opts = {})=>{
17
- const options = {
18
- ...defaults,
19
- ...opts
20
- };
21
- const fetch = (request, cachedValues = {})=>{
22
- const values = {};
23
- for (const internalKey of Object.keys(options.fetchData)){
24
- if (cachedValues[internalKey]) continue;
25
- values[internalKey] = client.send(new GetObjectCommand(options.fetchData[internalKey])).then(async (resp)=>{
26
- let value = await resp.Body.transformToString();
27
- if (contentTypePattern.test(resp.ContentType)) {
28
- value = jsonSafeParse(value);
29
- }
30
- return value;
31
- }).catch((e)=>{
32
- const value = getCache(options.cacheKey).value ?? {};
33
- value[internalKey] = undefined;
34
- modifyCache(options.cacheKey, value);
35
- throw e;
36
- });
37
- }
38
- return values;
39
- };
40
- let client;
41
- if (canPrefetch(options)) {
42
- client = createPrefetchClient(options);
43
- processCache(options, fetch);
13
+ AwsClient: S3Client,
14
+ awsClientOptions: {},
15
+ awsClientAssumeRole: undefined,
16
+ awsClientCapture: undefined,
17
+ fetchData: {},
18
+ disablePrefetch: false,
19
+ cacheKey: 's3',
20
+ cacheKeyExpiry: {},
21
+ cacheExpiry: -1,
22
+ setToContext: false
23
+ }
24
+ const contentTypePattern = /^application\/(.+\+)?json($|;.+)/
25
+ const s3Middleware = (opts = {}) => {
26
+ const options = {
27
+ ...defaults,
28
+ ...opts
29
+ }
30
+ const fetch = (request, cachedValues = {}) => {
31
+ const values = {}
32
+ for (const internalKey of Object.keys(options.fetchData)) {
33
+ if (cachedValues[internalKey]) continue
34
+ values[internalKey] = client
35
+ .send(new GetObjectCommand(options.fetchData[internalKey]))
36
+ .then(async (resp) => {
37
+ let value = await resp.Body.transformToString()
38
+ if (contentTypePattern.test(resp.ContentType)) {
39
+ value = jsonSafeParse(value)
40
+ }
41
+ return value
42
+ })
43
+ .catch((e) => {
44
+ const value = getCache(options.cacheKey).value ?? {}
45
+ value[internalKey] = undefined
46
+ modifyCache(options.cacheKey, value)
47
+ throw e
48
+ })
44
49
  }
45
- const s3MiddlewareBefore = async (request)=>{
46
- if (!client) {
47
- client = await createClient(options, request);
48
- }
49
- const { value } = processCache(options, fetch, request);
50
- Object.assign(request.internal, value);
51
- if (options.setToContext) {
52
- const data = await getInternal(Object.keys(options.fetchData), request);
53
- Object.assign(request.context, data);
54
- }
55
- };
56
- return {
57
- before: s3MiddlewareBefore
58
- };
59
- };
60
- export default s3Middleware;
50
+ return values
51
+ }
52
+ let client
53
+ if (canPrefetch(options)) {
54
+ client = createPrefetchClient(options)
55
+ processCache(options, fetch)
56
+ }
57
+ const s3MiddlewareBefore = async (request) => {
58
+ if (!client) {
59
+ client = await createClient(options, request)
60
+ }
61
+ const { value } = processCache(options, fetch, request)
62
+ Object.assign(request.internal, value)
63
+ if (options.setToContext) {
64
+ const data = await getInternal(Object.keys(options.fetchData), request)
65
+ Object.assign(request.context, data)
66
+ }
67
+ }
68
+ return {
69
+ before: s3MiddlewareBefore
70
+ }
71
+ }
72
+ export default s3Middleware
61
73
 
74
+ // used for TS type inference (see index.d.ts)
75
+ export function s3Req (req) {
76
+ return req
77
+ }
package/package.json CHANGED
@@ -1,33 +1,27 @@
1
1
  {
2
2
  "name": "@middy/s3",
3
- "version": "4.6.5",
3
+ "version": "5.0.0-alpha.1",
4
4
  "description": "S3 middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=18"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "./index.cjs",
14
13
  "module": "./index.js",
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": {
18
17
  "types": "./index.d.ts",
19
18
  "default": "./index.js"
20
- },
21
- "require": {
22
- "types": "./index.d.ts",
23
- "default": "./index.cjs"
24
19
  }
25
20
  }
26
21
  },
27
22
  "types": "index.d.ts",
28
23
  "files": [
29
24
  "index.js",
30
- "index.cjs",
31
25
  "index.d.ts"
32
26
  ],
33
27
  "scripts": {
@@ -64,13 +58,13 @@
64
58
  "url": "https://github.com/sponsors/willfarrell"
65
59
  },
66
60
  "dependencies": {
67
- "@middy/util": "4.6.5"
61
+ "@middy/util": "5.0.0-alpha.1"
68
62
  },
69
63
  "devDependencies": {
70
64
  "@aws-sdk/client-s3": "^3.0.0",
71
- "@middy/core": "4.6.5",
65
+ "@middy/core": "5.0.0-alpha.1",
72
66
  "@types/aws-lambda": "^8.10.101",
73
67
  "aws-xray-sdk": "^3.3.3"
74
68
  },
75
- "gitHead": "573d7b0bb243d8c5a9bcb00cf29d031aa7a0c606"
69
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
76
70
  }
package/index.cjs DELETED
@@ -1,71 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: function() {
8
- return _default;
9
- }
10
- });
11
- const _util = require("@middy/util");
12
- const _clients3 = require("@aws-sdk/client-s3");
13
- const defaults = {
14
- AwsClient: _clients3.S3Client,
15
- awsClientOptions: {},
16
- awsClientAssumeRole: undefined,
17
- awsClientCapture: undefined,
18
- fetchData: {},
19
- disablePrefetch: false,
20
- cacheKey: 's3',
21
- cacheKeyExpiry: {},
22
- cacheExpiry: -1,
23
- setToContext: false
24
- };
25
- const contentTypePattern = /^application\/(.+\+)?json($|;.+)/;
26
- const s3Middleware = (opts = {})=>{
27
- const options = {
28
- ...defaults,
29
- ...opts
30
- };
31
- const fetch = (request, cachedValues = {})=>{
32
- const values = {};
33
- for (const internalKey of Object.keys(options.fetchData)){
34
- if (cachedValues[internalKey]) continue;
35
- values[internalKey] = client.send(new _clients3.GetObjectCommand(options.fetchData[internalKey])).then(async (resp)=>{
36
- let value = await resp.Body.transformToString();
37
- if (contentTypePattern.test(resp.ContentType)) {
38
- value = (0, _util.jsonSafeParse)(value);
39
- }
40
- return value;
41
- }).catch((e)=>{
42
- const value = (0, _util.getCache)(options.cacheKey).value ?? {};
43
- value[internalKey] = undefined;
44
- (0, _util.modifyCache)(options.cacheKey, value);
45
- throw e;
46
- });
47
- }
48
- return values;
49
- };
50
- let client;
51
- if ((0, _util.canPrefetch)(options)) {
52
- client = (0, _util.createPrefetchClient)(options);
53
- (0, _util.processCache)(options, fetch);
54
- }
55
- const s3MiddlewareBefore = async (request)=>{
56
- if (!client) {
57
- client = await (0, _util.createClient)(options, request);
58
- }
59
- const { value } = (0, _util.processCache)(options, fetch, request);
60
- Object.assign(request.internal, value);
61
- if (options.setToContext) {
62
- const data = await (0, _util.getInternal)(Object.keys(options.fetchData), request);
63
- Object.assign(request.context, data);
64
- }
65
- };
66
- return {
67
- before: s3MiddlewareBefore
68
- };
69
- };
70
- const _default = s3Middleware;
71
-