@openstax/ts-utils 1.15.1 → 1.15.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.
- package/dist/cjs/services/authProvider/subrequest.js +3 -3
- package/dist/cjs/services/documentStore/unversioned/dynamodb.js +2 -1
- package/dist/cjs/services/documentStore/unversioned/file-system.js +3 -2
- package/dist/cjs/services/documentStore/versioned/file-system.d.ts +1 -1
- package/dist/cjs/services/documentStore/versioned/file-system.js +28 -99
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/authProvider/subrequest.js +3 -3
- package/dist/esm/services/documentStore/unversioned/dynamodb.js +2 -1
- package/dist/esm/services/documentStore/unversioned/file-system.js +3 -2
- package/dist/esm/services/documentStore/versioned/file-system.d.ts +1 -1
- package/dist/esm/services/documentStore/versioned/file-system.js +28 -73
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -17,12 +17,12 @@ export const subrequestAuthProvider = (initializer) => (configProvider) => {
|
|
|
17
17
|
return { headers };
|
|
18
18
|
};
|
|
19
19
|
const loadUser = async () => {
|
|
20
|
-
const
|
|
21
|
-
const [token] = getAuthTokenOrCookie(request,
|
|
20
|
+
const resolvedCookieName = await cookieName();
|
|
21
|
+
const [token] = getAuthTokenOrCookie(request, resolvedCookieName);
|
|
22
22
|
if (!token) {
|
|
23
23
|
return undefined;
|
|
24
24
|
}
|
|
25
|
-
const headers = { cookie: cookie.serialize(
|
|
25
|
+
const headers = { cookie: cookie.serialize(resolvedCookieName, token) };
|
|
26
26
|
return initializer.fetch((await accountsBase()).replace(/\/+$/, '') + '/api/user', { headers })
|
|
27
27
|
.then(response => response.json());
|
|
28
28
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BatchGetItemCommand, DynamoDB, GetItemCommand, PutItemCommand, ScanCommand, UpdateItemCommand } from '@aws-sdk/client-dynamodb';
|
|
2
2
|
import { once } from '../../..';
|
|
3
3
|
import { resolveConfigValue } from '../../../config';
|
|
4
|
+
import { NotFoundError } from '../../../errors';
|
|
4
5
|
import { ifDefined } from '../../../guards';
|
|
5
6
|
import { decodeDynamoDocument, encodeDynamoAttribute, encodeDynamoDocument } from '../dynamoEncoding';
|
|
6
7
|
const dynamodb = once(() => new DynamoDB({ apiVersion: '2012-08-10' }));
|
|
@@ -59,7 +60,7 @@ export const dynamoUnversionedDocumentStore = (initializer) => () => (configProv
|
|
|
59
60
|
var _a;
|
|
60
61
|
const result = (_a = item.Attributes) === null || _a === void 0 ? void 0 : _a[field]['N'];
|
|
61
62
|
if (!result) {
|
|
62
|
-
throw new
|
|
63
|
+
throw new NotFoundError(`Item with ${key} "${id}" does not exist`);
|
|
63
64
|
}
|
|
64
65
|
return parseFloat(result);
|
|
65
66
|
});
|
|
@@ -2,12 +2,13 @@ import * as fsModule from 'fs';
|
|
|
2
2
|
import path from 'path';
|
|
3
3
|
import { hashValue } from '../../..';
|
|
4
4
|
import { resolveConfigValue } from '../../../config';
|
|
5
|
+
import { NotFoundError } from '../../../errors';
|
|
5
6
|
import { ifDefined, isDefined } from '../../../guards';
|
|
6
7
|
export const fileSystemUnversionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey) => {
|
|
7
8
|
const tableName = resolveConfigValue(configProvider[initializer.configSpace || 'fileSystem'].tableName);
|
|
8
9
|
const tablePath = tableName.then((table) => path.join(initializer.dataDir, table));
|
|
9
10
|
const { mkdir, readdir, readFile, writeFile } = ifDefined(initializer.fs, fsModule);
|
|
10
|
-
const mkTableDir = new Promise((resolve, reject) => tablePath.then((path) => mkdir(path, { recursive: true }, (err) => err ? reject(err) : resolve())));
|
|
11
|
+
const mkTableDir = new Promise((resolve, reject) => tablePath.then((path) => mkdir(path, { recursive: true }, (err) => err && err.code !== 'EEXIST' ? reject(err) : resolve())));
|
|
11
12
|
const hashFilename = (value) => `${hashValue(value)}.json`;
|
|
12
13
|
const filePath = async (filename) => path.join(await tablePath, filename);
|
|
13
14
|
const load = async (filename) => {
|
|
@@ -72,7 +73,7 @@ export const fileSystemUnversionedDocumentStore = (initializer) => () => (config
|
|
|
72
73
|
await mkTableDir;
|
|
73
74
|
const data = await load(filename);
|
|
74
75
|
if (!data) {
|
|
75
|
-
throw new
|
|
76
|
+
throw new NotFoundError(`Item with ${hashKey.toString()} "${id}" does not exist`);
|
|
76
77
|
}
|
|
77
78
|
const newItem = { ...data, ...item };
|
|
78
79
|
return new Promise((resolve, reject) => {
|
|
@@ -3,7 +3,7 @@ import { ConfigProviderForConfig } from '../../../config';
|
|
|
3
3
|
import { VersionedDocumentAuthor, VersionedTDocument } from '.';
|
|
4
4
|
interface Initializer<C> {
|
|
5
5
|
dataDir: string;
|
|
6
|
-
fs?: Pick<typeof import('fs'), 'readFile' | 'writeFile'>;
|
|
6
|
+
fs?: Pick<typeof import('fs'), 'mkdir' | 'readdir' | 'readFile' | 'writeFile'>;
|
|
7
7
|
configSpace?: C;
|
|
8
8
|
}
|
|
9
9
|
export declare const fileSystemVersionedDocumentStore: <C extends string = "fileSystem">(initializer: Initializer<C>) => <T extends VersionedTDocument<T>>() => (configProvider: { [key in C]: {
|
|
@@ -1,65 +1,33 @@
|
|
|
1
|
-
import
|
|
2
|
-
import path from 'path';
|
|
3
|
-
import { hashValue } from '../../..';
|
|
4
|
-
import { resolveConfigValue } from '../../../config';
|
|
5
|
-
import { ifDefined } from '../../../guards';
|
|
1
|
+
import { fileSystemUnversionedDocumentStore } from '../unversioned/file-system';
|
|
6
2
|
const PAGE_LIMIT = 5;
|
|
7
3
|
export const fileSystemVersionedDocumentStore = (initializer) => () => (configProvider) => (_, hashKey, getAuthor) => {
|
|
8
|
-
const
|
|
9
|
-
const filePath = tableName.then((table) => path.join(initializer.dataDir, table));
|
|
10
|
-
const { readFile, writeFile } = ifDefined(initializer.fs, fsModule);
|
|
11
|
-
let data;
|
|
12
|
-
const load = filePath.then(path => new Promise(resolve => {
|
|
13
|
-
readFile(path, (err, readData) => {
|
|
14
|
-
if (err) {
|
|
15
|
-
console.error(err);
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
try {
|
|
19
|
-
data = JSON.parse(readData.toString());
|
|
20
|
-
if (typeof data !== 'object') {
|
|
21
|
-
data = undefined;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
catch (e) {
|
|
25
|
-
console.error(e);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
resolve();
|
|
29
|
-
});
|
|
30
|
-
}));
|
|
31
|
-
let previousSave;
|
|
4
|
+
const unversionedDocuments = fileSystemUnversionedDocumentStore(initializer)()(configProvider)({}, 'id');
|
|
32
5
|
return {
|
|
33
|
-
loadAllDocumentsTheBadWay:
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
return versionsList[versionsList.length - 1];
|
|
38
|
-
});
|
|
6
|
+
loadAllDocumentsTheBadWay: () => {
|
|
7
|
+
return unversionedDocuments.loadAllDocumentsTheBadWay().then(documents => documents.map(document => {
|
|
8
|
+
return document.items[document.items.length - 1];
|
|
9
|
+
}));
|
|
39
10
|
},
|
|
40
11
|
getVersions: async (id, startVersion) => {
|
|
41
|
-
await
|
|
42
|
-
const versions =
|
|
43
|
-
|
|
44
|
-
if (!versions || !versionsList) {
|
|
12
|
+
const item = await unversionedDocuments.getItem(id);
|
|
13
|
+
const versions = item === null || item === void 0 ? void 0 : item.items.reverse();
|
|
14
|
+
if (!versions) {
|
|
45
15
|
return undefined;
|
|
46
16
|
}
|
|
47
|
-
const startIndex = startVersion ?
|
|
48
|
-
const items =
|
|
49
|
-
const hasMore = (startIndex + 5) <
|
|
17
|
+
const startIndex = startVersion ? versions.findIndex(version => version.timestamp === startVersion) + 1 : 0;
|
|
18
|
+
const items = versions.slice(startIndex, startIndex + PAGE_LIMIT);
|
|
19
|
+
const hasMore = (startIndex + 5) < versions.length;
|
|
50
20
|
return {
|
|
51
21
|
items,
|
|
52
22
|
nextPageToken: hasMore ? items[items.length - 1].timestamp : undefined
|
|
53
23
|
};
|
|
54
24
|
},
|
|
55
25
|
getItem: async (id, timestamp) => {
|
|
56
|
-
await
|
|
57
|
-
const versions = data === null || data === void 0 ? void 0 : data[hashValue(id)];
|
|
26
|
+
const item = await unversionedDocuments.getItem(id);
|
|
58
27
|
if (timestamp) {
|
|
59
|
-
return
|
|
28
|
+
return item === null || item === void 0 ? void 0 : item.items.find(version => version.timestamp === timestamp);
|
|
60
29
|
}
|
|
61
|
-
|
|
62
|
-
return versionsList[versionsList.length - 1];
|
|
30
|
+
return item ? item.items[item.items.length - 1] : undefined;
|
|
63
31
|
},
|
|
64
32
|
prepareItem: async (item, ...authorArgs) => {
|
|
65
33
|
// this getAuthor type is terrible
|
|
@@ -68,36 +36,23 @@ export const fileSystemVersionedDocumentStore = (initializer) => () => (configPr
|
|
|
68
36
|
return {
|
|
69
37
|
document: { ...item, timestamp, author },
|
|
70
38
|
save: async (changes) => {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const hash = hashValue(document[hashKey]);
|
|
78
|
-
const versions = data[hash] = data[hash] || {};
|
|
79
|
-
versions[document.timestamp] = document;
|
|
80
|
-
writeFile(path, JSON.stringify(data, null, 2), () => resolve(document));
|
|
81
|
-
})());
|
|
82
|
-
return await save;
|
|
39
|
+
var _a;
|
|
40
|
+
const document = { ...item, ...changes, timestamp, author };
|
|
41
|
+
const container = (_a = await unversionedDocuments.getItem(document[hashKey])) !== null && _a !== void 0 ? _a : { id: document[hashKey], items: [] };
|
|
42
|
+
const updated = { ...container, items: [...container.items, document] };
|
|
43
|
+
await unversionedDocuments.putItem(updated);
|
|
44
|
+
return document;
|
|
83
45
|
}
|
|
84
46
|
};
|
|
85
47
|
},
|
|
86
48
|
putItem: async (item, ...authorArgs) => {
|
|
87
|
-
|
|
88
|
-
await
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const path = await filePath;
|
|
95
|
-
data = data || {};
|
|
96
|
-
const versions = data[hash] = data[hash] || {};
|
|
97
|
-
versions[document.timestamp] = document;
|
|
98
|
-
writeFile(path, JSON.stringify(data, null, 2), () => resolve(document));
|
|
99
|
-
})());
|
|
100
|
-
return await save;
|
|
49
|
+
var _a;
|
|
50
|
+
const author = getAuthor ? await getAuthor(...authorArgs) : authorArgs[0];
|
|
51
|
+
const document = { ...item, timestamp: new Date().getTime(), author };
|
|
52
|
+
const container = (_a = await unversionedDocuments.getItem(document[hashKey])) !== null && _a !== void 0 ? _a : { id: document[hashKey], items: [] };
|
|
53
|
+
const updated = { ...container, items: [...container.items, document] };
|
|
54
|
+
await unversionedDocuments.putItem(updated);
|
|
55
|
+
return document;
|
|
101
56
|
},
|
|
102
57
|
};
|
|
103
58
|
};
|