ag-common 0.0.356 → 0.0.358

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.
@@ -47,3 +47,25 @@ export declare const deleteFiles: ({ Bucket, Keys, }: {
47
47
  error?: string;
48
48
  }>;
49
49
  export declare function listFiles(bucketName: string): Promise<string[]>;
50
+ /**
51
+ * allow uploading of file directly to s3
52
+ * @param param0
53
+ *
54
+ * @returns url to POST to, and fields to send, eg
55
+ * formData.append('Content-Type', file.type);
56
+ Object.entries(fields).forEach(([k, v]) => {
57
+ formData.append(k, v);
58
+ });
59
+
60
+ formData.append('file', file);
61
+ fetch.POST(url,formData)
62
+ */
63
+ export declare function getPresignedPostURL({ bucket, key, maxMb, }: {
64
+ bucket: string;
65
+ key: string;
66
+ /** max filesize. default 5 */
67
+ maxMb?: number;
68
+ }): Promise<{
69
+ url: string;
70
+ fields: Record<string, string>;
71
+ }>;
@@ -24,7 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  return (mod && mod.__esModule) ? mod : { "default": mod };
25
25
  };
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.listFiles = exports.deleteFiles = exports.deleteFile = exports.uploadFile = exports.putS3Object = exports.getS3Objects = exports.getS3Object = exports.setS3 = void 0;
27
+ exports.getPresignedPostURL = exports.listFiles = exports.deleteFiles = exports.deleteFile = exports.uploadFile = exports.putS3Object = exports.getS3Objects = exports.getS3Object = exports.setS3 = void 0;
28
28
  const s3_1 = __importDefault(require("aws-sdk/clients/s3"));
29
29
  const log_1 = require("../../common/helpers/log");
30
30
  const array_1 = require("../../common/helpers/array");
@@ -139,3 +139,34 @@ function listFiles(bucketName) {
139
139
  });
140
140
  }
141
141
  exports.listFiles = listFiles;
142
+ /**
143
+ * allow uploading of file directly to s3
144
+ * @param param0
145
+ *
146
+ * @returns url to POST to, and fields to send, eg
147
+ * formData.append('Content-Type', file.type);
148
+ Object.entries(fields).forEach(([k, v]) => {
149
+ formData.append(k, v);
150
+ });
151
+
152
+ formData.append('file', file);
153
+ fetch.POST(url,formData)
154
+ */
155
+ function getPresignedPostURL({ bucket, key, maxMb = 5, }) {
156
+ return __awaiter(this, void 0, void 0, function* () {
157
+ const ps = yield s3.createPresignedPost({
158
+ Bucket: bucket,
159
+ Fields: {
160
+ Key: key,
161
+ },
162
+ Expires: 600,
163
+ Conditions: [
164
+ ['content-length-range', 0, maxMb * 1049000],
165
+ ['starts-with', '$Content-Type', 'image/'],
166
+ ],
167
+ });
168
+ const fields = JSON.parse(JSON.stringify(ps.fields));
169
+ return { fields, url: ps.url };
170
+ });
171
+ }
172
+ exports.getPresignedPostURL = getPresignedPostURL;
@@ -44,7 +44,7 @@ const getOperation = ({ path, method, resource, schema, }) => {
44
44
  return { operation, pathParams };
45
45
  };
46
46
  function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID, jwksRegion = 'ap-southeast-2', getAndValidateTokenOverride, }) {
47
- var _a, _b, _c, _d, _e;
47
+ var _a, _b, _c, _d, _e, _f, _g;
48
48
  return __awaiter(this, void 0, void 0, function* () {
49
49
  if (!schema) {
50
50
  throw new Error('schema undefined!');
@@ -86,13 +86,15 @@ function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID
86
86
  else {
87
87
  try {
88
88
  request.params = opm.pathParams;
89
- (0, log_1.info)('req=', JSON.stringify(request, null, 2));
89
+ (0, log_1.info)('req=', JSON.stringify(Object.assign(Object.assign({}, request), { body: ((_b = (_a = request.body) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) > 2000
90
+ ? '(truncating long body)'
91
+ : request.body }), null, 2));
90
92
  const resp = new openapi_request_validator_1.default(Object.assign(Object.assign({}, opm.operation), { schemas: schema.components.schemas })).validateRequest(request);
91
93
  if (resp) {
92
94
  (0, log_1.warn)('bad request');
93
95
  (0, log_1.warn)('opm=', JSON.stringify(opm, null, 2));
94
96
  (0, log_1.warn)('resp=', JSON.stringify(resp, null, 2));
95
- return (0, api_1.returnCode)(400, `error:${(_b = (_a = resp === null || resp === void 0 ? void 0 : resp.errors) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.message}`);
97
+ return (0, api_1.returnCode)(400, `error:${(_d = (_c = resp === null || resp === void 0 ? void 0 : resp.errors) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.message}`);
96
98
  }
97
99
  (0, log_1.debug)(`validated request:`, event.path);
98
100
  }
@@ -102,7 +104,7 @@ function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID
102
104
  }
103
105
  let userProfile;
104
106
  let error;
105
- const authHeader = ((_c = event.headers) === null || _c === void 0 ? void 0 : _c.Authorization) || ((_d = event.headers) === null || _d === void 0 ? void 0 : _d.authorization);
107
+ const authHeader = ((_e = event.headers) === null || _e === void 0 ? void 0 : _e.Authorization) || ((_f = event.headers) === null || _f === void 0 ? void 0 : _f.authorization);
106
108
  if (authorized === true || (authorized === 'optional' && authHeader)) {
107
109
  const vf = getAndValidateTokenOverride !== null && getAndValidateTokenOverride !== void 0 ? getAndValidateTokenOverride : validations_1.getAndValidateToken;
108
110
  ({ error, userProfile } = yield vf({
@@ -120,7 +122,7 @@ function validateOpenApi({ event, next, authorized, schema, COGNITO_USER_POOL_ID
120
122
  event,
121
123
  body: (0, object_1.tryJsonParse)(event.body, event.body),
122
124
  userProfile,
123
- lang: (0, i18n_1.getValidatedLang)((_e = event.headers['x-lang']) !== null && _e !== void 0 ? _e : ''),
125
+ lang: (0, i18n_1.getValidatedLang)((_g = event.headers['x-lang']) !== null && _g !== void 0 ? _g : ''),
124
126
  });
125
127
  return res;
126
128
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-common",
3
- "version": "0.0.356",
3
+ "version": "0.0.358",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "author": "Andrei Gec <@andreigec> (https://gec.dev/)",