@openstax/ts-utils 1.27.1 → 1.27.3

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 (25) hide show
  1. package/dist/cjs/middleware/lambdaCorsResponseMiddleware.js +3 -1
  2. package/dist/cjs/services/accountsGateway/index.js +5 -4
  3. package/dist/cjs/services/apiGateway/index.d.ts +9 -10
  4. package/dist/cjs/services/documentStore/unversioned/dynamodb.d.ts +2 -0
  5. package/dist/cjs/services/documentStore/unversioned/dynamodb.js +145 -143
  6. package/dist/cjs/services/documentStore/versioned/dynamodb.d.ts +2 -0
  7. package/dist/cjs/services/documentStore/versioned/dynamodb.js +125 -123
  8. package/dist/cjs/services/fileServer/s3FileServer.d.ts +1 -1
  9. package/dist/cjs/services/fileServer/s3FileServer.js +5 -2
  10. package/dist/cjs/services/launchParams/verifier.js +3 -1
  11. package/dist/cjs/services/searchProvider/index.d.ts +4 -0
  12. package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
  13. package/dist/esm/middleware/lambdaCorsResponseMiddleware.js +3 -1
  14. package/dist/esm/services/accountsGateway/index.js +5 -4
  15. package/dist/esm/services/apiGateway/index.d.ts +9 -10
  16. package/dist/esm/services/documentStore/unversioned/dynamodb.d.ts +2 -0
  17. package/dist/esm/services/documentStore/unversioned/dynamodb.js +145 -143
  18. package/dist/esm/services/documentStore/versioned/dynamodb.d.ts +2 -0
  19. package/dist/esm/services/documentStore/versioned/dynamodb.js +125 -123
  20. package/dist/esm/services/fileServer/s3FileServer.d.ts +1 -1
  21. package/dist/esm/services/fileServer/s3FileServer.js +5 -2
  22. package/dist/esm/services/launchParams/verifier.js +3 -1
  23. package/dist/esm/services/searchProvider/index.d.ts +4 -0
  24. package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
  25. package/package.json +1 -1
@@ -6,136 +6,138 @@ const __1 = require("../../..");
6
6
  const config_1 = require("../../../config");
7
7
  const guards_1 = require("../../../guards");
8
8
  const dynamoEncoding_1 = require("../dynamoEncoding");
9
- const dynamodb = (0, __1.once)(() => new client_dynamodb_1.DynamoDB({ apiVersion: '2012-08-10' }));
10
9
  // i'm not really excited about getAuthor being required, but ts is getting confused about the type when unspecified
11
- const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
10
+ const dynamoVersionedDocumentStore = (initializer) => {
12
11
  const init = (0, guards_1.ifDefined)(initializer, {});
13
- const tableName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(configProvider[(0, guards_1.ifDefined)(init.configSpace, 'dynamodb')].tableName));
14
- return {
15
- loadAllDocumentsTheBadWay: async () => {
16
- const loadAllResults = async (ExclusiveStartKey) => {
17
- var _a;
18
- const cmd = new client_dynamodb_1.ScanCommand({ TableName: await tableName(), ExclusiveStartKey });
19
- const result = await dynamodb().send(cmd);
20
- const resultItems = ((_a = result.Items) === null || _a === void 0 ? void 0 : _a.map(dynamoEncoding_1.decodeDynamoDocument)) || [];
21
- if (result.LastEvaluatedKey) {
22
- return [...resultItems, ...await loadAllResults(result.LastEvaluatedKey)];
23
- }
24
- return resultItems;
25
- };
26
- const allResults = await loadAllResults().then(results => results.reduce((result, document) => {
27
- const current = result.get(document[hashKey]);
28
- if (!current || current.timestamp < document.timestamp) {
29
- return result.set(document[hashKey], document);
30
- }
31
- return result;
32
- }, new Map()));
33
- return Array.from(allResults.values());
34
- },
35
- getVersions: async (id, startVersion) => {
36
- const cmd = new client_dynamodb_1.QueryCommand({
37
- TableName: await tableName(),
38
- KeyConditionExpression: '#hk = :hkv',
39
- ExpressionAttributeValues: {
40
- ':hkv': (0, dynamoEncoding_1.encodeDynamoAttribute)(id)
41
- },
42
- ExpressionAttributeNames: {
12
+ const dynamodb = (0, __1.once)(() => { var _a; return (_a = init.dynamoClient) !== null && _a !== void 0 ? _a : new client_dynamodb_1.DynamoDB({ apiVersion: '2012-08-10' }); });
13
+ return () => (configProvider) => (_, hashKey, options) => {
14
+ const tableName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(configProvider[(0, guards_1.ifDefined)(init.configSpace, 'dynamodb')].tableName));
15
+ return {
16
+ loadAllDocumentsTheBadWay: async () => {
17
+ const loadAllResults = async (ExclusiveStartKey) => {
18
+ var _a;
19
+ const cmd = new client_dynamodb_1.ScanCommand({ TableName: await tableName(), ExclusiveStartKey });
20
+ const result = await dynamodb().send(cmd);
21
+ const resultItems = ((_a = result.Items) === null || _a === void 0 ? void 0 : _a.map(dynamoEncoding_1.decodeDynamoDocument)) || [];
22
+ if (result.LastEvaluatedKey) {
23
+ return [...resultItems, ...await loadAllResults(result.LastEvaluatedKey)];
24
+ }
25
+ return resultItems;
26
+ };
27
+ const allResults = await loadAllResults().then(results => results.reduce((result, document) => {
28
+ const current = result.get(document[hashKey]);
29
+ if (!current || current.timestamp < document.timestamp) {
30
+ return result.set(document[hashKey], document);
31
+ }
32
+ return result;
33
+ }, new Map()));
34
+ return Array.from(allResults.values());
35
+ },
36
+ getVersions: async (id, startVersion) => {
37
+ const cmd = new client_dynamodb_1.QueryCommand({
38
+ TableName: await tableName(),
39
+ KeyConditionExpression: '#hk = :hkv',
40
+ ExpressionAttributeValues: {
41
+ ':hkv': (0, dynamoEncoding_1.encodeDynamoAttribute)(id)
42
+ },
43
+ ExpressionAttributeNames: {
44
+ '#hk': hashKey.toString()
45
+ },
46
+ ...(startVersion
47
+ ? { ExclusiveStartKey: {
48
+ [hashKey]: (0, dynamoEncoding_1.encodeDynamoAttribute)(id),
49
+ timestamp: { N: startVersion.toString() }
50
+ } }
51
+ : {}),
52
+ ScanIndexForward: false,
53
+ });
54
+ return dynamodb().send(cmd).then(result => {
55
+ var _a;
56
+ const items = (_a = result.Items) === null || _a === void 0 ? void 0 : _a.map(dynamoEncoding_1.decodeDynamoDocument);
57
+ if (!items || items.length === 0) {
58
+ return undefined;
59
+ }
60
+ return {
61
+ items,
62
+ nextPageToken: result.LastEvaluatedKey ? (0, dynamoEncoding_1.decodeDynamoDocument)(result.LastEvaluatedKey).timestamp : undefined
63
+ };
64
+ });
65
+ },
66
+ getItem: async (id, timestamp) => {
67
+ let keyConditionExpression = '#hk = :hkv';
68
+ const expressionAttributeNames = {
43
69
  '#hk': hashKey.toString()
44
- },
45
- ...(startVersion
46
- ? { ExclusiveStartKey: {
47
- [hashKey]: (0, dynamoEncoding_1.encodeDynamoAttribute)(id),
48
- timestamp: { N: startVersion.toString() }
49
- } }
50
- : {}),
51
- ScanIndexForward: false,
52
- });
53
- return dynamodb().send(cmd).then(result => {
54
- var _a;
55
- const items = (_a = result.Items) === null || _a === void 0 ? void 0 : _a.map(dynamoEncoding_1.decodeDynamoDocument);
56
- if (!items || items.length === 0) {
57
- return undefined;
70
+ };
71
+ const expressionAttributeValues = {
72
+ ':hkv': (0, dynamoEncoding_1.encodeDynamoAttribute)(id)
73
+ };
74
+ if (timestamp) {
75
+ keyConditionExpression += ' and #ts = :tsv';
76
+ expressionAttributeNames['#ts'] = 'timestamp';
77
+ expressionAttributeValues[':tsv'] = (0, dynamoEncoding_1.encodeDynamoAttribute)(timestamp);
58
78
  }
79
+ const cmd = new client_dynamodb_1.QueryCommand({
80
+ TableName: await tableName(),
81
+ KeyConditionExpression: keyConditionExpression,
82
+ ExpressionAttributeNames: expressionAttributeNames,
83
+ ExpressionAttributeValues: expressionAttributeValues,
84
+ ScanIndexForward: false,
85
+ Limit: 1
86
+ });
87
+ return dynamodb().send(cmd).then(result => {
88
+ var _a;
89
+ return (_a = result.Items) === null || _a === void 0 ? void 0 : _a.map(dynamoEncoding_1.decodeDynamoDocument)[0];
90
+ });
91
+ },
92
+ /* prepares a new version of a document with the given data, then allows some additional
93
+ * changes to be input to a `save` function that actually saves it. useful for additional
94
+ * changes based on the new document version or author. the document version and author
95
+ * cannot be modified */
96
+ prepareItem: async (item, ...authorArgs) => {
97
+ // this getAuthor type is terrible
98
+ const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
99
+ const timestamp = new Date().getTime();
59
100
  return {
60
- items,
61
- nextPageToken: result.LastEvaluatedKey ? (0, dynamoEncoding_1.decodeDynamoDocument)(result.LastEvaluatedKey).timestamp : undefined
101
+ document: { ...item, timestamp, author },
102
+ save: async (changes) => {
103
+ var _a;
104
+ const document = {
105
+ ...item,
106
+ ...changes,
107
+ timestamp,
108
+ author
109
+ };
110
+ const cmd = new client_dynamodb_1.PutItemCommand({
111
+ TableName: await tableName(),
112
+ Item: (0, dynamoEncoding_1.encodeDynamoDocument)(document),
113
+ });
114
+ const updatedDoc = await dynamodb().send(cmd)
115
+ .then(() => document);
116
+ await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
117
+ return updatedDoc;
118
+ }
62
119
  };
63
- });
64
- },
65
- getItem: async (id, timestamp) => {
66
- let keyConditionExpression = '#hk = :hkv';
67
- const expressionAttributeNames = {
68
- '#hk': hashKey.toString()
69
- };
70
- const expressionAttributeValues = {
71
- ':hkv': (0, dynamoEncoding_1.encodeDynamoAttribute)(id)
72
- };
73
- if (timestamp) {
74
- keyConditionExpression += ' and #ts = :tsv';
75
- expressionAttributeNames['#ts'] = 'timestamp';
76
- expressionAttributeValues[':tsv'] = (0, dynamoEncoding_1.encodeDynamoAttribute)(timestamp);
77
- }
78
- const cmd = new client_dynamodb_1.QueryCommand({
79
- TableName: await tableName(),
80
- KeyConditionExpression: keyConditionExpression,
81
- ExpressionAttributeNames: expressionAttributeNames,
82
- ExpressionAttributeValues: expressionAttributeValues,
83
- ScanIndexForward: false,
84
- Limit: 1
85
- });
86
- return dynamodb().send(cmd).then(result => {
120
+ },
121
+ /* saves a new version of a document with the given data */
122
+ putItem: async (item, ...authorArgs) => {
87
123
  var _a;
88
- return (_a = result.Items) === null || _a === void 0 ? void 0 : _a.map(dynamoEncoding_1.decodeDynamoDocument)[0];
89
- });
90
- },
91
- /* prepares a new version of a document with the given data, then allows some additional
92
- * changes to be input to a `save` function that actually saves it. useful for additional
93
- * changes based on the new document version or author. the document version and author
94
- * cannot be modified */
95
- prepareItem: async (item, ...authorArgs) => {
96
- // this getAuthor type is terrible
97
- const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
98
- const timestamp = new Date().getTime();
99
- return {
100
- document: { ...item, timestamp, author },
101
- save: async (changes) => {
102
- var _a;
103
- const document = {
104
- ...item,
105
- ...changes,
106
- timestamp,
107
- author
108
- };
109
- const cmd = new client_dynamodb_1.PutItemCommand({
110
- TableName: await tableName(),
111
- Item: (0, dynamoEncoding_1.encodeDynamoDocument)(document),
112
- });
113
- const updatedDoc = await dynamodb().send(cmd)
114
- .then(() => document);
115
- await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
116
- return updatedDoc;
117
- }
118
- };
119
- },
120
- /* saves a new version of a document with the given data */
121
- putItem: async (item, ...authorArgs) => {
122
- var _a;
123
- // this getAuthor type is terrible
124
- const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
125
- const document = {
126
- ...item,
127
- timestamp: new Date().getTime(),
128
- author
129
- };
130
- const cmd = new client_dynamodb_1.PutItemCommand({
131
- TableName: await tableName(),
132
- Item: (0, dynamoEncoding_1.encodeDynamoDocument)(document),
133
- });
134
- const updatedDoc = await dynamodb().send(cmd)
135
- .then(() => document);
136
- await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
137
- return updatedDoc;
138
- },
124
+ // this getAuthor type is terrible
125
+ const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
126
+ const document = {
127
+ ...item,
128
+ timestamp: new Date().getTime(),
129
+ author
130
+ };
131
+ const cmd = new client_dynamodb_1.PutItemCommand({
132
+ TableName: await tableName(),
133
+ Item: (0, dynamoEncoding_1.encodeDynamoDocument)(document),
134
+ });
135
+ const updatedDoc = await dynamodb().send(cmd)
136
+ .then(() => document);
137
+ await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
138
+ return updatedDoc;
139
+ },
140
+ };
139
141
  };
140
142
  };
141
143
  exports.dynamoVersionedDocumentStore = dynamoVersionedDocumentStore;
@@ -7,7 +7,7 @@ export declare type Config = {
7
7
  };
8
8
  interface Initializer<C> {
9
9
  configSpace?: C;
10
- s3Client?: typeof S3Client;
10
+ getS3Client?: (...args: ConstructorParameters<typeof S3Client>) => S3Client;
11
11
  }
12
12
  export declare const s3FileServer: <C extends string = "deployed">(initializer: Initializer<C>) => (configProvider: { [key in C]: {
13
13
  bucketName: import("../../config").ConfigValueProvider<string>;
@@ -12,8 +12,11 @@ const s3FileServer = (initializer) => (configProvider) => {
12
12
  const config = configProvider[(0, guards_1.ifDefined)(initializer.configSpace, 'deployed')];
13
13
  const bucketName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(config.bucketName));
14
14
  const bucketRegion = (0, __1.once)(() => (0, config_1.resolveConfigValue)(config.bucketRegion));
15
- const client = (0, guards_1.ifDefined)(initializer.s3Client, client_s3_1.S3Client);
16
- const s3Service = (0, __1.once)(async () => new client({ apiVersion: '2012-08-10', region: await bucketRegion() }));
15
+ const s3Service = (0, __1.once)(async () => {
16
+ var _a, _b;
17
+ const args = { apiVersion: '2012-08-10', region: await bucketRegion() };
18
+ return (_b = (_a = initializer.getS3Client) === null || _a === void 0 ? void 0 : _a.call(initializer, args)) !== null && _b !== void 0 ? _b : new client_s3_1.S3Client(args);
19
+ });
17
20
  /*
18
21
  * https://docs.aws.amazon.com/AmazonS3/latest/userguide/ShareObjectPreSignedURL.html
19
22
  */
@@ -30,11 +30,13 @@ const __1 = require("../..");
30
30
  const config_1 = require("../../config");
31
31
  const errors_1 = require("../../errors");
32
32
  const guards_1 = require("../../guards");
33
+ const helpers_1 = require("../../misc/helpers");
33
34
  /**
34
35
  * Creates a class that can verify launch params
35
36
  */
36
37
  const createLaunchVerifier = ({ configSpace, fetcher }) => (configProvider) => {
37
38
  const config = configProvider[(0, guards_1.ifDefined)(configSpace, 'launch')];
39
+ const getTrustedDomain = (0, helpers_1.once)(() => (0, config_1.resolveConfigValue)(config.trustedDomain));
38
40
  const getJwksClient = (0, __1.memoize)((jwksUri) => new jwks_rsa_1.JwksClient({ fetcher, jwksUri }));
39
41
  const getJwksKey = (0, __1.memoize)(async (jwksUri, kid) => {
40
42
  const client = getJwksClient(jwksUri);
@@ -50,7 +52,7 @@ const createLaunchVerifier = ({ configSpace, fetcher }) => (configProvider) => {
50
52
  try {
51
53
  const jwksUrl = new URL('/.well-known/jwks.json', iss);
52
54
  const launchDomain = jwksUrl.hostname;
53
- const trustedDomain = await (0, config_1.resolveConfigValue)(config.trustedDomain);
55
+ const trustedDomain = await getTrustedDomain();
54
56
  if (launchDomain !== trustedDomain && !launchDomain.endsWith(`.${trustedDomain}`)) {
55
57
  return callback(new Error(`Untrusted launch domain: "${launchDomain}"`));
56
58
  }
@@ -31,6 +31,10 @@ declare type Field = {
31
31
  } | {
32
32
  key: string;
33
33
  type: 'keyword';
34
+ } | {
35
+ key: string;
36
+ type: 'date';
37
+ format?: string;
34
38
  } | {
35
39
  key: string;
36
40
  type: 'boolean';