@openstax/ts-utils 1.24.5 → 1.25.1-pre1

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.
@@ -79,9 +79,6 @@ const makeRouteClient = (initializer, config, route, appProvider) => {
79
79
  if (response.status === 401) {
80
80
  throw new errors_1.UnauthorizedError();
81
81
  }
82
- if (response.status === 403) {
83
- throw new errors_1.ForbiddenError();
84
- }
85
82
  if (response.status === 440) {
86
83
  throw new errors_1.SessionExpiredError();
87
84
  }
@@ -5,7 +5,9 @@ interface Initializer<C> {
5
5
  }
6
6
  export declare const dynamoUnversionedDocumentStore: <C extends string = "dynamodb">(initializer?: Initializer<C> | undefined) => <T extends TDocument<T>>() => (configProvider: { [key in C]: {
7
7
  tableName: import("../../../config").ConfigValueProvider<string>;
8
- }; }) => <K extends keyof T>(_: {}, hashKey: K) => {
8
+ }; }) => <K extends keyof T>(_: {}, hashKey: K, options?: {
9
+ afterWrite?: ((item: T) => void | Promise<void>) | undefined;
10
+ } | undefined) => {
9
11
  loadAllDocumentsTheBadWay: () => Promise<T[]>;
10
12
  batchGetItem: (ids: T[K][]) => Promise<T[]>;
11
13
  getItem: (id: T[K]) => Promise<T | undefined>;
@@ -8,9 +8,9 @@ const errors_1 = require("../../../errors");
8
8
  const guards_1 = require("../../../guards");
9
9
  const dynamoEncoding_1 = require("../dynamoEncoding");
10
10
  const dynamodb = (0, __1.once)(() => new client_dynamodb_1.DynamoDB({ apiVersion: '2012-08-10' }));
11
- const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey) => {
12
- const options = (0, guards_1.ifDefined)(initializer, {});
13
- const tableName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(configProvider[(0, guards_1.ifDefined)(options.configSpace, 'dynamodb')].tableName));
11
+ const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
12
+ 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
14
  return {
15
15
  loadAllDocumentsTheBadWay: async () => {
16
16
  const loadAllResults = async (ExclusiveStartKey) => {
@@ -57,14 +57,19 @@ const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) =
57
57
  ConditionExpression: 'attribute_exists(#k)',
58
58
  ExpressionAttributeNames: { '#k': hashKey.toString(), '#f': field },
59
59
  ExpressionAttributeValues: { ':one': { N: '1' } },
60
- ReturnValues: 'UPDATED_NEW',
60
+ ReturnValues: 'ALL_NEW',
61
61
  });
62
- return dynamodb().send(cmd).then((item) => {
63
- var _a;
64
- const result = (_a = item.Attributes) === null || _a === void 0 ? void 0 : _a[field]['N'];
65
- if (!result) {
62
+ return dynamodb().send(cmd).then(async (item) => {
63
+ var _a, _b, _c;
64
+ const result = (_b = (_a = item.Attributes) === null || _a === void 0 ? void 0 : _a[field]) === null || _b === void 0 ? void 0 : _b['N'];
65
+ if (!item.Attributes) {
66
66
  throw new errors_1.NotFoundError(`Item with ${key} "${id}" does not exist`);
67
67
  }
68
+ if (!result) {
69
+ throw new errors_1.NotFoundError(`Item with ${key} "${id}" did not produce field "${field}"`);
70
+ }
71
+ const updatedDoc = (0, dynamoEncoding_1.decodeDynamoDocument)(item.Attributes);
72
+ await ((_c = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _c === void 0 ? void 0 : _c.call(options, updatedDoc));
68
73
  return parseFloat(result);
69
74
  }).catch((error) => {
70
75
  throw error.name === 'ConditionalCheckFailedException' ?
@@ -99,11 +104,14 @@ const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) =
99
104
  ExpressionAttributeValues: expressionAttributeValues,
100
105
  ReturnValues: 'ALL_NEW',
101
106
  });
102
- return dynamodb().send(cmd).then((item) => {
107
+ return dynamodb().send(cmd).then(async (item) => {
108
+ var _a;
103
109
  if (!item.Attributes) {
104
110
  throw new errors_1.NotFoundError(`Item with ${key} "${id}" does not exist`);
105
111
  }
106
- return (0, dynamoEncoding_1.decodeDynamoDocument)(item.Attributes);
112
+ const updatedDoc = (0, dynamoEncoding_1.decodeDynamoDocument)(item.Attributes);
113
+ await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
114
+ return updatedDoc;
107
115
  }).catch((error) => {
108
116
  throw error.name === 'ConditionalCheckFailedException' ?
109
117
  new errors_1.NotFoundError(`Item with ${key} "${id}" does not exist`) : error;
@@ -111,11 +119,14 @@ const dynamoUnversionedDocumentStore = (initializer) => () => (configProvider) =
111
119
  },
112
120
  /* replaces the entire document with the given data */
113
121
  putItem: async (item) => {
122
+ var _a;
114
123
  const cmd = new client_dynamodb_1.PutItemCommand({
115
124
  TableName: await tableName(),
116
125
  Item: (0, dynamoEncoding_1.encodeDynamoDocument)(item),
117
126
  });
118
- return dynamodb().send(cmd).then(() => item);
127
+ const updatedDoc = await dynamodb().send(cmd).then(() => item);
128
+ await ((_a = options === null || options === void 0 ? void 0 : options.afterWrite) === null || _a === void 0 ? void 0 : _a.call(options, updatedDoc));
129
+ return updatedDoc;
119
130
  },
120
131
  };
121
132
  };
@@ -6,7 +6,10 @@ interface Initializer<C> {
6
6
  }
7
7
  export declare const dynamoVersionedDocumentStore: <C extends string = "dynamodb">(initializer?: Initializer<C> | undefined) => <T extends VersionedTDocument<T>>() => (configProvider: { [key in C]: {
8
8
  tableName: import("../../../config").ConfigValueProvider<string>;
9
- }; }) => <K extends keyof T, A extends ((...a: any[]) => Promise<VersionedDocumentAuthor>) | undefined>(_: {}, hashKey: K, getAuthor: A) => {
9
+ }; }) => <K extends keyof T, A extends ((...a: any[]) => Promise<VersionedDocumentAuthor>) | undefined>(_: {}, hashKey: K, options?: {
10
+ afterWrite?: ((item: T) => void | Promise<void>) | undefined;
11
+ getAuthor?: A | undefined;
12
+ } | undefined) => {
10
13
  loadAllDocumentsTheBadWay: () => Promise<T[]>;
11
14
  getVersions: (id: T[K], startVersion?: number | undefined) => Promise<{
12
15
  items: T[];
@@ -8,9 +8,9 @@ const guards_1 = require("../../../guards");
8
8
  const dynamoEncoding_1 = require("../dynamoEncoding");
9
9
  const dynamodb = (0, __1.once)(() => new client_dynamodb_1.DynamoDB({ apiVersion: '2012-08-10' }));
10
10
  // 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, getAuthor) => {
12
- const options = (0, guards_1.ifDefined)(initializer, {});
13
- const tableName = (0, __1.once)(() => (0, config_1.resolveConfigValue)(configProvider[(0, guards_1.ifDefined)(options.configSpace, 'dynamodb')].tableName));
11
+ const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, options) => {
12
+ 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
14
  return {
15
15
  loadAllDocumentsTheBadWay: async () => {
16
16
  const loadAllResults = async (ExclusiveStartKey) => {
@@ -94,11 +94,12 @@ const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) =>
94
94
  * cannot be modified */
95
95
  prepareItem: async (item, ...authorArgs) => {
96
96
  // this getAuthor type is terrible
97
- const author = getAuthor ? await getAuthor(...authorArgs) : authorArgs[0];
97
+ const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
98
98
  const timestamp = new Date().getTime();
99
99
  return {
100
100
  document: { ...item, timestamp, author },
101
101
  save: async (changes) => {
102
+ var _a;
102
103
  const document = {
103
104
  ...item,
104
105
  ...changes,
@@ -109,15 +110,18 @@ const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) =>
109
110
  TableName: await tableName(),
110
111
  Item: (0, dynamoEncoding_1.encodeDynamoDocument)(document),
111
112
  });
112
- return dynamodb().send(cmd)
113
+ const updatedDoc = await dynamodb().send(cmd)
113
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;
114
117
  }
115
118
  };
116
119
  },
117
120
  /* saves a new version of a document with the given data */
118
121
  putItem: async (item, ...authorArgs) => {
122
+ var _a;
119
123
  // this getAuthor type is terrible
120
- const author = getAuthor ? await getAuthor(...authorArgs) : authorArgs[0];
124
+ const author = (options === null || options === void 0 ? void 0 : options.getAuthor) ? await options.getAuthor(...authorArgs) : authorArgs[0];
121
125
  const document = {
122
126
  ...item,
123
127
  timestamp: new Date().getTime(),
@@ -127,8 +131,10 @@ const dynamoVersionedDocumentStore = (initializer) => () => (configProvider) =>
127
131
  TableName: await tableName(),
128
132
  Item: (0, dynamoEncoding_1.encodeDynamoDocument)(document),
129
133
  });
130
- return dynamodb().send(cmd)
134
+ const updatedDoc = await dynamodb().send(cmd)
131
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;
132
138
  },
133
139
  };
134
140
  };