@openstax/ts-utils 1.1.26 → 1.1.28

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 (117) hide show
  1. package/dist/{assertions.d.ts → cjs/assertions.d.ts} +0 -0
  2. package/dist/{assertions.js → cjs/assertions.js} +0 -0
  3. package/dist/{aws → cjs/aws}/securityTokenService.d.ts +0 -0
  4. package/dist/{aws → cjs/aws}/securityTokenService.js +0 -0
  5. package/dist/{aws → cjs/aws}/ssmService.d.ts +0 -0
  6. package/dist/{aws → cjs/aws}/ssmService.js +0 -0
  7. package/dist/{config.d.ts → cjs/config.d.ts} +0 -0
  8. package/dist/{config.js → cjs/config.js} +0 -0
  9. package/dist/{errors.d.ts → cjs/errors.d.ts} +0 -0
  10. package/dist/{errors.js → cjs/errors.js} +0 -0
  11. package/dist/{fetch.d.ts → cjs/fetch.d.ts} +0 -0
  12. package/dist/{fetch.js → cjs/fetch.js} +0 -0
  13. package/dist/{guards.d.ts → cjs/guards.d.ts} +0 -0
  14. package/dist/{guards.js → cjs/guards.js} +0 -0
  15. package/dist/{index.d.ts → cjs/index.d.ts} +0 -0
  16. package/dist/{index.js → cjs/index.js} +0 -0
  17. package/dist/{middleware.d.ts → cjs/middleware.d.ts} +0 -0
  18. package/dist/{middleware.js → cjs/middleware.js} +0 -0
  19. package/dist/{pagination.d.ts → cjs/pagination.d.ts} +0 -0
  20. package/dist/{pagination.js → cjs/pagination.js} +0 -0
  21. package/dist/{profile.d.ts → cjs/profile.d.ts} +0 -0
  22. package/dist/{profile.js → cjs/profile.js} +0 -0
  23. package/dist/{routing.d.ts → cjs/routing.d.ts} +0 -0
  24. package/dist/{routing.js → cjs/routing.js} +0 -0
  25. package/dist/{services → cjs/services}/apiGateway/index.d.ts +0 -0
  26. package/dist/{services → cjs/services}/apiGateway/index.js +0 -0
  27. package/dist/{services → cjs/services}/authProvider/browser.d.ts +0 -0
  28. package/dist/{services → cjs/services}/authProvider/browser.js +0 -0
  29. package/dist/{services → cjs/services}/authProvider/decryption.d.ts +0 -0
  30. package/dist/{services → cjs/services}/authProvider/decryption.js +0 -0
  31. package/dist/{services → cjs/services}/authProvider/index.d.ts +0 -0
  32. package/dist/{services → cjs/services}/authProvider/index.js +0 -0
  33. package/dist/{services → cjs/services}/authProvider/subrequest.d.ts +0 -0
  34. package/dist/{services → cjs/services}/authProvider/subrequest.js +0 -0
  35. package/dist/{services → cjs/services}/authProvider/utils/embeddedAuthProvider.d.ts +0 -0
  36. package/dist/{services → cjs/services}/authProvider/utils/embeddedAuthProvider.js +0 -0
  37. package/dist/{services → cjs/services}/exercisesGateway/index.d.ts +0 -0
  38. package/dist/{services → cjs/services}/exercisesGateway/index.js +0 -0
  39. package/dist/{services → cjs/services}/lrsGateway/attempt-utils.d.ts +0 -0
  40. package/dist/{services → cjs/services}/lrsGateway/attempt-utils.js +0 -0
  41. package/dist/{services → cjs/services}/lrsGateway/file-system.d.ts +0 -0
  42. package/dist/{services → cjs/services}/lrsGateway/file-system.js +0 -0
  43. package/dist/{services → cjs/services}/lrsGateway/index.d.ts +0 -0
  44. package/dist/{services → cjs/services}/lrsGateway/index.js +0 -0
  45. package/dist/{services → cjs/services}/searchProvider/index.d.ts +0 -0
  46. package/dist/{services → cjs/services}/searchProvider/index.js +0 -0
  47. package/dist/{services → cjs/services}/searchProvider/memorySearchTheBadWay.d.ts +0 -0
  48. package/dist/{services → cjs/services}/searchProvider/memorySearchTheBadWay.js +0 -0
  49. package/dist/{services → cjs/services}/versionedDocumentStore/dynamodb.d.ts +0 -0
  50. package/dist/{services → cjs/services}/versionedDocumentStore/dynamodb.js +0 -0
  51. package/dist/{services → cjs/services}/versionedDocumentStore/file-system.d.ts +0 -0
  52. package/dist/{services → cjs/services}/versionedDocumentStore/file-system.js +0 -0
  53. package/dist/{services → cjs/services}/versionedDocumentStore/index.d.ts +0 -0
  54. package/dist/{services → cjs/services}/versionedDocumentStore/index.js +0 -0
  55. package/dist/cjs/tsconfig.withoutspecs.cjs.tsbuildinfo +1 -0
  56. package/dist/{types.d.ts → cjs/types.d.ts} +0 -0
  57. package/dist/{types.js → cjs/types.js} +0 -0
  58. package/dist/esm/assertions.d.ts +9 -0
  59. package/dist/esm/assertions.js +90 -0
  60. package/dist/esm/aws/securityTokenService.d.ts +2 -0
  61. package/dist/esm/aws/securityTokenService.js +3 -0
  62. package/dist/esm/aws/ssmService.d.ts +2 -0
  63. package/dist/esm/aws/ssmService.js +3 -0
  64. package/dist/esm/config.d.ts +27 -0
  65. package/dist/esm/config.js +127 -0
  66. package/dist/esm/errors.d.ts +12 -0
  67. package/dist/esm/errors.js +26 -0
  68. package/dist/esm/fetch.d.ts +64 -0
  69. package/dist/esm/fetch.js +46 -0
  70. package/dist/esm/guards.d.ts +6 -0
  71. package/dist/esm/guards.js +29 -0
  72. package/dist/esm/index.d.ts +29 -0
  73. package/dist/esm/index.js +210 -0
  74. package/dist/esm/middleware.d.ts +9 -0
  75. package/dist/esm/middleware.js +34 -0
  76. package/dist/esm/pagination.d.ts +63 -0
  77. package/dist/esm/pagination.js +77 -0
  78. package/dist/esm/profile.d.ts +59 -0
  79. package/dist/esm/profile.js +191 -0
  80. package/dist/esm/routing.d.ts +107 -0
  81. package/dist/esm/routing.js +208 -0
  82. package/dist/esm/services/apiGateway/index.d.ts +55 -0
  83. package/dist/esm/services/apiGateway/index.js +51 -0
  84. package/dist/esm/services/authProvider/browser.d.ts +61 -0
  85. package/dist/esm/services/authProvider/browser.js +119 -0
  86. package/dist/esm/services/authProvider/decryption.d.ts +16 -0
  87. package/dist/esm/services/authProvider/decryption.js +61 -0
  88. package/dist/esm/services/authProvider/index.d.ts +42 -0
  89. package/dist/esm/services/authProvider/index.js +15 -0
  90. package/dist/esm/services/authProvider/subrequest.d.ts +16 -0
  91. package/dist/esm/services/authProvider/subrequest.js +36 -0
  92. package/dist/esm/services/authProvider/utils/embeddedAuthProvider.d.ts +20 -0
  93. package/dist/esm/services/authProvider/utils/embeddedAuthProvider.js +30 -0
  94. package/dist/esm/services/exercisesGateway/index.d.ts +74 -0
  95. package/dist/esm/services/exercisesGateway/index.js +69 -0
  96. package/dist/esm/services/lrsGateway/attempt-utils.d.ts +62 -0
  97. package/dist/esm/services/lrsGateway/attempt-utils.js +251 -0
  98. package/dist/esm/services/lrsGateway/file-system.d.ts +15 -0
  99. package/dist/esm/services/lrsGateway/file-system.js +96 -0
  100. package/dist/esm/services/lrsGateway/index.d.ts +110 -0
  101. package/dist/esm/services/lrsGateway/index.js +87 -0
  102. package/dist/esm/services/searchProvider/index.d.ts +19 -0
  103. package/dist/esm/services/searchProvider/index.js +1 -0
  104. package/dist/esm/services/searchProvider/memorySearchTheBadWay.d.ts +12 -0
  105. package/dist/esm/services/searchProvider/memorySearchTheBadWay.js +53 -0
  106. package/dist/esm/services/versionedDocumentStore/dynamodb.d.ts +23 -0
  107. package/dist/esm/services/versionedDocumentStore/dynamodb.js +147 -0
  108. package/dist/esm/services/versionedDocumentStore/file-system.d.ts +25 -0
  109. package/dist/esm/services/versionedDocumentStore/file-system.js +81 -0
  110. package/dist/esm/services/versionedDocumentStore/index.d.ts +23 -0
  111. package/dist/esm/services/versionedDocumentStore/index.js +1 -0
  112. package/dist/esm/tsconfig.withoutspecs.esm.tsbuildinfo +1 -0
  113. package/dist/esm/types.d.ts +6 -0
  114. package/dist/esm/types.js +1 -0
  115. package/package.json +25 -8
  116. package/dist/tsconfig.tsbuildinfo +0 -1
  117. package/dist/tsconfig.withoutspecs.tsbuildinfo +0 -1
@@ -0,0 +1,210 @@
1
+ import { createHash } from 'crypto';
2
+ import deepEqual from 'deep-equal';
3
+ import { isPlainObject } from './guards';
4
+ /*
5
+ * there was a reason i made these instead of using lodash/fp but i forget what it was. i think maybe
6
+ * these do more validation that the second function gets a compatible object.
7
+ *
8
+ * const getAuthor = getKeyValue('author');
9
+ * const author = getAuthor(book);
10
+ *
11
+ * const getAuthorOrNope = getKeyValueOr('author', 'nope');
12
+ * const authorOrNope = getAuthorOrNope(book);
13
+ *
14
+ * const putAuthor = putKeyValue('author');
15
+ * const newBook = putAuthor(book, 'tom');
16
+ * */
17
+ export const getKeyValue = (key) => (obj) => obj[key];
18
+ export const getKeyValueOr = (key, defaultValue) => (obj) => obj[key] || defaultValue;
19
+ export const putKeyValue = (key) => (obj, value) => ({ ...obj, [key]: value });
20
+ export const coerceArray = (thing) => thing instanceof Array ? thing : thing !== undefined ? [thing] : [];
21
+ /*
22
+ * this is like lodash/flow but it uses a recursive type instead of hard-coding parameters
23
+ */
24
+ export const flow = (...chain) => (param) => {
25
+ let result = param;
26
+ for (const fn of chain) {
27
+ result = fn(result);
28
+ }
29
+ return result;
30
+ };
31
+ /*
32
+ * a shameful helper to avoid needing to test code coverage of branches
33
+ */
34
+ export const fnIf = (condition, trueValue, falseValue) => condition ? trueValue : falseValue;
35
+ /*
36
+ * maps the array and returns the first result that matches the predicate
37
+ * avoids processing extra elements that would happen with .map().find() or .reduce
38
+ *
39
+ * eg the third element of the array is never processed:
40
+ * const result = mapFind([1,2,3], x => 'hello'.charAt(x), x => x === 'l');
41
+ */
42
+ export const mapFind = (array, mapper, predicate = (r) => !!r) => {
43
+ for (const item of array) {
44
+ const mapped = mapper(item);
45
+ if (predicate(mapped)) {
46
+ return mapped;
47
+ }
48
+ }
49
+ };
50
+ /*
51
+ * creates a string hash of lots of different kinds of things.
52
+ *
53
+ * eg:
54
+ * hashValue({someKey: 'someValue'})
55
+ * */
56
+ export const hashValue = (value) => {
57
+ // hack for sorting keys https://stackoverflow.com/a/53593328/14809536
58
+ const allKeys = new Set();
59
+ JSON.stringify(value, (k, v) => (allKeys.add(k), v));
60
+ const strValue = JSON.stringify(value, Array.from(allKeys).sort());
61
+ return createHash('sha1').update(strValue).digest('hex');
62
+ };
63
+ /*
64
+ * returns a function that will only ever call the given function once, returning the first result for every subsequent call
65
+ *
66
+ * eg:
67
+ * const heavyFunction = () => 'hello';
68
+ * const butOnlyOnce = once(() => 'hello');
69
+ *
70
+ * heavyFunction() // returns `hello`;
71
+ * butOnlyOnce() // returns `hello`;
72
+ */
73
+ export const once = (fn) => {
74
+ const initialValue = {};
75
+ let result = initialValue;
76
+ return ((...args) => result === initialValue ? (result = fn(...args)) : result);
77
+ };
78
+ /*
79
+ * partitions a sequence based on a partition function returning {value: any; matches?: boolean}
80
+ * - if the function returns `matches` explicitly then adjacent matching elements will
81
+ * be grouped and the predicate value in the result will be from the last item in the group
82
+ * - if the function returns only a value then matching will be evaluated based on the deep
83
+ * equality of the value with its neighbors
84
+ *
85
+ * this is different from lodash/partition and lodash/groupBy because:
86
+ * - it preserves the order of the items, items will only be grouped if they are already adjacent
87
+ * - there can be any number of groups
88
+ * - it tells you the partition value
89
+ * - the partition value can be reduced, if you care (so you can like, partition on sequential values)
90
+ *
91
+ * simple predicate:
92
+ * returns: [[0, [1,2]], [1, [3,4,5]]]
93
+ * partitionSequence((n: number) => ({value: Math.floor(n / 3)}), [1,2,3,4,5])
94
+ *
95
+ * mutating partition:
96
+ * returns: [
97
+ * [{min: 1,max: 3}, [1,2,3]],
98
+ * [{min: 5,max: 6}, [5,6]],
99
+ * [{min: 8,max: 8}, [8]],
100
+ * ]
101
+ * partitionSequence(
102
+ * (n: number, p?: {min: number; max: number}) =>
103
+ * p && p.max + 1 === n
104
+ * ? {value: {...p, max: n}, matches: true}
105
+ * : {value: {min: n, max: n}, matches: false}
106
+ * , [1,2,3,5,6,8]
107
+ * )
108
+ */
109
+ export const partitionSequence = (getPartition, sequence) => {
110
+ const appendItem = (result, item) => {
111
+ const current = result[result.length - 1];
112
+ const itemPartition = getPartition(item, current === null || current === void 0 ? void 0 : current[0]);
113
+ if (current && ((itemPartition.matches === undefined && deepEqual(current[0], itemPartition.value))
114
+ || itemPartition.matches)) {
115
+ current[0] = itemPartition.value;
116
+ current[1].push(item);
117
+ }
118
+ else {
119
+ result.push([itemPartition.value, [item]]);
120
+ }
121
+ return result;
122
+ };
123
+ return sequence.reduce(appendItem, []);
124
+ };
125
+ /*
126
+ * memoizes a function with any number of arguments
127
+ */
128
+ export const memoize = (fn) => {
129
+ const cache = {};
130
+ const resolveCache = (cacheLayer, [first, ...rest]) => {
131
+ if (!first) {
132
+ return cacheLayer;
133
+ }
134
+ const layers = first instanceof Object
135
+ ? (cacheLayer.weakLayers = (cacheLayer.weakLayers || (typeof WeakMap === 'undefined' ? undefined : new WeakMap())))
136
+ : (cacheLayer.strongLayers = (cacheLayer.strongLayers || new Map()));
137
+ // argument is an object and WeakMap is not supported
138
+ if (!layers) {
139
+ return {};
140
+ }
141
+ const layer = layers.get(first) || {};
142
+ if (!layers.has(first)) {
143
+ layers.set(first, layer);
144
+ }
145
+ return resolveCache(layer, rest);
146
+ };
147
+ return ((...args) => {
148
+ const thisCache = resolveCache(cache, args);
149
+ return thisCache.result = (thisCache.result || fn(...args));
150
+ });
151
+ };
152
+ /*
153
+ * rounds number to given number of places
154
+ *
155
+ * eg:
156
+ * roundToPrecision(1234.123, 2); // returns 1200
157
+ * roundToPrecision(1234.123, -2); // returns 1234.12
158
+ * roundToPrecision(1234.125, -2); // returns 1234.13
159
+ */
160
+ export const roundToPrecision = (num, places) => {
161
+ const multiplier = Math.pow(10, -1 * places);
162
+ // https://stackoverflow.com/a/11832950/14809536
163
+ return Math.round((num + Number.EPSILON) * multiplier) / multiplier;
164
+ };
165
+ export const getCommonProperties = (thing1, thing2) => Object.keys(thing1).filter((key) => Object.keys(thing2).includes(key));
166
+ /*
167
+ * recursive merge properties of inputs. values are merged if they are
168
+ * plain objects or arrays, otherwise if the same property exists in both
169
+ * objects the value from the second argument will win.
170
+ *
171
+ * unlike lodash merge, this will not change object references for values that
172
+ * exist only in one parameter.
173
+ *
174
+ * eg:
175
+ * merge({thing: 'one'}, {thing: 'two', otherKey: 'one'}, {coolKey: 'coolValue'});
176
+ */
177
+ export const merge = (...[thing1, ...tail]) => {
178
+ const mergedTail = tail.length > 0
179
+ ? merge(...tail)
180
+ : null;
181
+ if (!mergedTail) {
182
+ return thing1;
183
+ }
184
+ return {
185
+ ...thing1,
186
+ ...mergedTail,
187
+ ...getCommonProperties(thing1, mergedTail).reduce((result, property) => ({
188
+ ...result,
189
+ ...(isPlainObject(thing1[property]) && isPlainObject(mergedTail[property])
190
+ ? { [property]: merge(thing1[property], mergedTail[property]) }
191
+ : (Array.isArray(thing1[property]) && Array.isArray(mergedTail[property]))
192
+ ? { [property]: [...thing1[property], ...mergedTail[property]] }
193
+ : {}),
194
+ }), {}),
195
+ };
196
+ };
197
+ /*
198
+ * a silly utility to help typescript realize an array is a tuple
199
+ *
200
+ * eg:
201
+ * const a = [5, 'string'] // type is `Array<string | number>`
202
+ * const t = tuple(5, 'string') type is `[5, 'string']`
203
+ *
204
+ * both have the same javascript value, but one is forced to be a tuple, which
205
+ * is nice if its structure is important. examples are like the React.useState
206
+ * pattern where there are two return values in a tuple, or if you're feeding
207
+ * Object.fromEntries
208
+ *
209
+ */
210
+ export const tuple = (...args) => args;
@@ -0,0 +1,9 @@
1
+ import { TupleExtends } from './types';
2
+ export declare type MiddlewareProvider<Sa, M, A extends any[], R> = (app: Sa, appBinder?: ((app: Sa, provider: MiddlewareProvider<Sa, M, A, R>) => (middleware: M, ...args: A) => R)) => (middleware: M, ...args: A) => R;
3
+ export declare type MiddlewareTransformProvider<Sa, M, A extends any[], R> = (app: Sa) => (middleware: M, ...args: A) => Omit<M, keyof R> & R;
4
+ export declare type MiddlewareInput<S> = S extends MiddlewareProvider<any, infer M, any, any> ? M : never;
5
+ export declare type ServiceMiddlewareProviderResult<M, S> = [M] extends [never] ? never : [M] extends [MiddlewareInput<S>] ? S extends MiddlewareTransformProvider<any, M, any, infer R> ? (Omit<R, keyof M> & M) extends R ? Omit<R, keyof M> & M : R extends M ? R : Omit<M, keyof R> & R : S extends MiddlewareProvider<any, M, any, infer R> ? R : never : never;
6
+ export declare type ServiceMiddlewareProviderArgs<S> = S extends MiddlewareProvider<any, any, infer A, any> ? A : never;
7
+ export declare type ServiceMiddlewareProviderChainResult<C, M> = C extends [infer S1, ...infer S] ? S extends never[] ? ServiceMiddlewareProviderResult<M, S1> : ServiceMiddlewareProviderChainResult<S, ServiceMiddlewareProviderResult<M, S1>> : C extends unknown ? M : never;
8
+ export declare type ServiceMiddlewareProviderChainArgs<C> = C extends [infer S1, ...infer S] ? S extends never[] ? ServiceMiddlewareProviderArgs<S1> : TupleExtends<ServiceMiddlewareProviderChainArgs<S>, ServiceMiddlewareProviderArgs<S1>> extends 'yes' ? ServiceMiddlewareProviderChainArgs<S> : TupleExtends<ServiceMiddlewareProviderArgs<S1>, ServiceMiddlewareProviderChainArgs<S>> extends 'yes' ? ServiceMiddlewareProviderArgs<S1> : never : C extends unknown ? [] : never;
9
+ export declare const makeComposeMiddleware: <Sa, M>() => <C extends any[]>(...chain: C) => MiddlewareProvider<Sa, M, ServiceMiddlewareProviderChainArgs<C>, ServiceMiddlewareProviderChainResult<C, M>>;
@@ -0,0 +1,34 @@
1
+ /*
2
+ * this function creates a middleware composer for the given AppServices and starting value. the ending
3
+ * value will depend on the composed values. after composing the values, the composer returns a function that
4
+ * expects the given input value and returns the result of the last element of the middleware chain.
5
+ *
6
+ * eg:
7
+ * export const composeServiceMiddleware = makeComposeMiddleware<AppServices, {request: ApiRouteRequest}>();
8
+ * export const composeResponseMiddleware = makeComposeMiddleware<AppServices, Promise<ApiRouteResponse> | undefined>();
9
+ *
10
+ * eg:
11
+ * // `requestServiceProvider` is a function that expects `{request: ApiRouteResponse}` (specified in the call to `makeComposeMiddleware`)
12
+ * // for requestServiceProviders as part of routes in particular, the request responder calls this when it resolves the routes.
13
+ * const requestServiceProvider = composeServiceMiddleware(
14
+ * cookieAuthMiddleware, // this one just depends on `request`, which is in the input, and returns that plus the authProvider
15
+ * myDocumentStoreMiddleware, // this one expects the authProvider, and adds the document store
16
+ * myDocumentSearchMiddleware, // this one expects the document store, and adds the search provider
17
+ * ); // if you try to specify them in the wrong order it'll yell at you
18
+ *
19
+ * WARNING: depending on how you use it, typescript _sometimes_ explodes on these recursive types. if you have problems in your build
20
+ * with memory, you will have to specify the expected result type when calling the middleware reducer. this is a little annoying,
21
+ * but it will still correctly error if the type you specify is wrong, so you've still got the type safety.
22
+ *
23
+ * // this might work, or it might make your compiler run out of memory
24
+ * eg: const myThing = makeComposeMiddleware()(...);
25
+ *
26
+ * // this helps typescript figure out what is going on
27
+ * eg: const myThing: myType = makeComposeMiddleware()(...);
28
+ * */
29
+ export const makeComposeMiddleware = () => (...chain) => (app, appBinder) => {
30
+ const boundChain = chain.map(provider => appBinder ? appBinder(app, provider) : provider(app));
31
+ return (middleware, ...args) => {
32
+ return boundChain.reduce((result, provider) => provider(result, ...args), middleware);
33
+ };
34
+ };
@@ -0,0 +1,63 @@
1
+ import { QueryParams, RouteMatchRecord } from './routing';
2
+ export declare type PaginationHandler<Pa, Q extends QueryParams> = <R>(queryParams: Q, match: RouteMatchRecord<R>) => Pa & {
3
+ getUnusedQueryParams: () => Q;
4
+ };
5
+ export declare const createPaginationMiddleware: <Ri, Q extends QueryParams = {
6
+ [key: string]: string | undefined;
7
+ }, R = any>() => <Pa>({ getQueryParams, setUnusedQueryParams, paginator }: {
8
+ getQueryParams: (request: Ri) => Q;
9
+ setUnusedQueryParams: (request: Ri, query: Q) => Ri;
10
+ paginator: PaginationHandler<Pa, Q>;
11
+ }) => <M extends {
12
+ request: Ri;
13
+ }>() => (middleware: M, match: RouteMatchRecord<R>) => M & {
14
+ pagination: Pa & {
15
+ getUnusedQueryParams: () => Q;
16
+ };
17
+ };
18
+ export declare const loadMorePagination: <R, Q extends QueryParams>(queryParams: Q, { route, params }: RouteMatchRecord<R>) => {
19
+ getUnusedQueryParams: () => Omit<Q, "pageToken">;
20
+ getPageTokenString: () => string | string[] | null | undefined;
21
+ getPageTokenNumber: () => number | undefined;
22
+ getPaginationResponse: <T>({ items, ...meta }: LoadMorePaginationResultInput<T>) => {
23
+ items: T[];
24
+ meta: {
25
+ nextPageToken: string | number | undefined;
26
+ };
27
+ links: {
28
+ nextPage: string | undefined;
29
+ };
30
+ };
31
+ };
32
+ export interface LoadMorePaginationResultInput<T> {
33
+ items: T[];
34
+ nextPageToken: string | number | undefined;
35
+ }
36
+ export declare const pageNumberPagination: <R, Q extends QueryParams>(queryParams: Q, { route, params }: RouteMatchRecord<R>) => {
37
+ getUnusedQueryParams: () => Omit<Q, "page">;
38
+ getPaginationParams: () => {
39
+ page: number | undefined;
40
+ };
41
+ getPaginationResponse: <T>({ items, ...meta }: PageNumberPaginationResultInput<T>) => {
42
+ items: T[];
43
+ meta: {
44
+ pageSize: number;
45
+ currentPage: number;
46
+ totalItems: number;
47
+ totalPages: number;
48
+ };
49
+ links: {
50
+ firstPage: string;
51
+ lastPage: string;
52
+ nextPage: string | undefined;
53
+ prevPage: string | undefined;
54
+ };
55
+ };
56
+ };
57
+ export interface PageNumberPaginationResultInput<T> {
58
+ items: T[];
59
+ pageSize: number;
60
+ currentPage: number;
61
+ totalItems: number;
62
+ totalPages: number;
63
+ }
@@ -0,0 +1,77 @@
1
+ import { notNaN } from './assertions';
2
+ import { InvalidRequestError } from './errors';
3
+ import { renderAnyRouteUrl } from './routing';
4
+ /*
5
+ * helper to create middleware with the given paginator. aside from taking care of annoying to write pagination logic, these helpers also make
6
+ * sure that all item list responses have the same formatting.
7
+ *
8
+ * eg:
9
+ * const getQueryParams = getKeyValueOr('queryStringParameters', {} as QueryParams);
10
+ * const setUnusedQueryParams = putKeyValue('queryStringParameters');
11
+ *
12
+ * export const loadMorePaginationMiddleware = createPaginationMiddleware<ApiRouteRequest>()({getQueryParams, setUnusedQueryParams, paginator: loadMorePagination});
13
+ * export const pageNumberPaginationMiddleware = createPaginationMiddleware<ApiRouteRequest>()({getQueryParams, setUnusedQueryParams, paginator: pageNumberPagination});
14
+ *
15
+ * eg the pagination middleware then provides your necessary inputs (getPageToken... in this case) and formats the response:
16
+ * const result = await services.myDocumentStore.getVersions(key, services.pagination.getPageTokenNumber());
17
+ *
18
+ * if (!result) {
19
+ * throw new NotFoundError('requested item not found');
20
+ * }
21
+ *
22
+ * return apiJsonResponse(200, services.pagination.getPaginationResponse(result));
23
+ */
24
+ export const createPaginationMiddleware = () => ({ getQueryParams, setUnusedQueryParams, paginator }) => () => (middleware, match) => {
25
+ const queryParams = getQueryParams(middleware.request);
26
+ const pagination = paginator(queryParams, match);
27
+ // remove pagination params from downstream logic
28
+ middleware.request = setUnusedQueryParams(middleware.request, pagination.getUnusedQueryParams());
29
+ return { ...middleware, pagination };
30
+ };
31
+ export const loadMorePagination = (queryParams, { route, params }) => {
32
+ const { pageToken, ...otherParams } = queryParams;
33
+ return {
34
+ getUnusedQueryParams: () => otherParams,
35
+ getPageTokenString: () => pageToken,
36
+ getPageTokenNumber: () => pageToken && typeof pageToken === 'string'
37
+ ? notNaN(parseInt(pageToken, 10), () => { throw new InvalidRequestError(); })
38
+ : undefined,
39
+ getPaginationResponse: ({ items, ...meta }) => {
40
+ return {
41
+ items,
42
+ meta,
43
+ links: {
44
+ nextPage: meta.nextPageToken
45
+ ? renderAnyRouteUrl(route, params, { ...queryParams, pageToken: meta.nextPageToken.toString() })
46
+ : undefined,
47
+ }
48
+ };
49
+ },
50
+ };
51
+ };
52
+ export const pageNumberPagination = (queryParams, { route, params }) => {
53
+ const { page, ...otherParams } = queryParams;
54
+ const numberPage = page && typeof page === 'string'
55
+ ? notNaN(parseInt(page, 10), () => { throw new InvalidRequestError(); })
56
+ : undefined;
57
+ return {
58
+ getUnusedQueryParams: () => otherParams,
59
+ getPaginationParams: () => ({ page: numberPage }),
60
+ getPaginationResponse: ({ items, ...meta }) => {
61
+ return {
62
+ items,
63
+ meta,
64
+ links: {
65
+ firstPage: renderAnyRouteUrl(route, params, { ...queryParams, page: '1' }),
66
+ lastPage: renderAnyRouteUrl(route, params, { ...queryParams, page: meta.totalPages.toString() }),
67
+ nextPage: meta.currentPage < meta.totalPages
68
+ ? renderAnyRouteUrl(route, params, { ...queryParams, page: (meta.currentPage + 1).toString() })
69
+ : undefined,
70
+ prevPage: meta.currentPage > 1
71
+ ? renderAnyRouteUrl(route, params, { ...queryParams, page: (meta.currentPage - 1).toString() })
72
+ : undefined
73
+ }
74
+ };
75
+ },
76
+ };
77
+ };
@@ -0,0 +1,59 @@
1
+ import { GenericFetch } from './fetch';
2
+ import { ApiResponse, HttpHeaders } from './routing';
3
+ export interface Call {
4
+ start: number;
5
+ end?: number;
6
+ children: Array<Profile | ProfileData>;
7
+ }
8
+ export declare type ProfileFunction = <Fn extends (...args: any[]) => any>(trackName: string, fn: (p: Track) => Fn) => Fn;
9
+ export interface Track {
10
+ track: ProfileFunction;
11
+ trackFetch: <F extends GenericFetch>(fetch: F) => F;
12
+ setTracing: (tracing: boolean | Promise<boolean>) => void;
13
+ getTracing: () => Promise<undefined | boolean>;
14
+ data: () => Call;
15
+ report: () => CallReport;
16
+ adopt: (child: Profile) => void;
17
+ }
18
+ export interface ProfileData {
19
+ name: string;
20
+ calls: Call[];
21
+ }
22
+ export interface Profile extends ProfileData {
23
+ report: () => ProfileReport;
24
+ start: () => Track & {
25
+ end: () => void;
26
+ };
27
+ }
28
+ interface CallReport {
29
+ time: number | string;
30
+ children: ProfileReport[];
31
+ coverage: 'unknown' | {
32
+ accountedTime: number;
33
+ unaccountedTime: number;
34
+ accountedPercent: number;
35
+ };
36
+ }
37
+ interface ProfileReport {
38
+ name: string;
39
+ time: 'unknown' | {
40
+ total: number;
41
+ min: number;
42
+ max: number;
43
+ avg: number;
44
+ };
45
+ calls: CallReport[];
46
+ }
47
+ export declare const createProfile: (name: string, options?: {
48
+ trace?: boolean | Promise<boolean>;
49
+ }) => Profile;
50
+ export declare const makeProfileApiResponseMiddleware: <Ri extends {
51
+ headers: HttpHeaders;
52
+ }>(showReport: (request: Ri) => string | boolean) => () => (responsePromise: Promise<ApiResponse<number, any>> | undefined, { request, profile }: {
53
+ request: Ri;
54
+ profile: Track;
55
+ }) => Promise<ApiResponse<number, any>> | undefined;
56
+ export declare const makeHtmlReport: (report: CallReport) => string;
57
+ export declare const makeHtmlProfileReport: (report: ProfileReport) => string;
58
+ export declare const makeHtmlCallReport: (report: CallReport, index: number) => string;
59
+ export {};
@@ -0,0 +1,191 @@
1
+ import { isNumber } from './guards';
2
+ import { apiHtmlResponse, apiJsonResponse, getHeader } from './routing';
3
+ import { merge, roundToPrecision } from '.';
4
+ const callReport = (call) => {
5
+ const time = call.start && call.end
6
+ ? call.end - call.start
7
+ : 'unknown';
8
+ const children = call.children
9
+ .filter(profile => profile.calls.length > 0)
10
+ .map(profileReport);
11
+ const accountedTime = children.reduce((result, profile) => typeof profile.time === 'string' ? result : result + profile.time.total, 0);
12
+ const coverage = typeof time === 'number' && children.length > 0 ? {
13
+ accountedTime,
14
+ unaccountedTime: time - accountedTime,
15
+ accountedPercent: roundToPrecision(accountedTime / time, -2),
16
+ } : 'unknown';
17
+ return { time, children, coverage };
18
+ };
19
+ const profileReport = (profile) => {
20
+ const calls = profile.calls.map(callReport);
21
+ const times = calls
22
+ .map(call => call.time)
23
+ .filter(isNumber);
24
+ const time = times.length > 0 ? {
25
+ total: times.reduce((result, item) => result + item, 0),
26
+ min: Math.min(...times),
27
+ max: Math.max(...times),
28
+ avg: Math.round(times.reduce((result, item) => result + item, 0) / times.length),
29
+ } : 'unknown';
30
+ return {
31
+ name: profile.name, calls, time
32
+ };
33
+ };
34
+ export const createProfile = (name, options = {}) => {
35
+ const profile = {
36
+ name,
37
+ calls: [],
38
+ report: () => profileReport(profile),
39
+ start: () => {
40
+ const call = { start: Date.now(), children: [] };
41
+ profile.calls.push(call);
42
+ return {
43
+ end: () => { call.end = Date.now(); },
44
+ report: () => callReport({ end: Date.now(), ...call }),
45
+ data: () => ({ end: Date.now(), ...call }),
46
+ adopt: (childProfile) => { call.children.push(childProfile); },
47
+ setTracing: (tracing) => { options.trace = tracing; },
48
+ getTracing: async () => options.trace,
49
+ trackFetch: (fetch) => {
50
+ const trackProfile = createProfile('fetch', options);
51
+ call.children.push(trackProfile);
52
+ return (async (url, config) => {
53
+ const { end, ...track } = trackProfile.start();
54
+ const fetchConfig = await options.trace
55
+ ? merge(config || {}, {
56
+ headers: { 'x-application-profile': 'trace' }
57
+ })
58
+ : config;
59
+ return fetch(url, fetchConfig).then(response => {
60
+ const fetchProfileJson = response.headers.get('x-application-profile');
61
+ const fetchProfile = fetchProfileJson ? JSON.parse(fetchProfileJson) : undefined;
62
+ end();
63
+ if (fetchProfile) {
64
+ const subrequestProfile = createProfile(url, options);
65
+ subrequestProfile.calls.push(fetchProfile);
66
+ track.adopt(subrequestProfile);
67
+ }
68
+ return response;
69
+ });
70
+ });
71
+ },
72
+ track: (trackName, fn) => {
73
+ const trackProfile = createProfile(trackName, options);
74
+ call.children.push(trackProfile);
75
+ return ((...args) => {
76
+ const { end, ...track } = trackProfile.start();
77
+ const result = fn(track)(...args);
78
+ if (result instanceof Promise) {
79
+ return result.finally(() => { end(); });
80
+ }
81
+ else {
82
+ end();
83
+ return result;
84
+ }
85
+ });
86
+ }
87
+ };
88
+ },
89
+ };
90
+ return profile;
91
+ };
92
+ export const makeProfileApiResponseMiddleware = (showReport) => () => (responsePromise, { request, profile }) => {
93
+ if (responsePromise) {
94
+ return responsePromise.then(async (response) => {
95
+ const reportFormat = showReport(request);
96
+ const enabled = await profile.getTracing();
97
+ // profiling is disabled, return original response
98
+ if (!enabled) {
99
+ return response;
100
+ }
101
+ // there is no report format, return original response but
102
+ // add sub-request trace response if necessary
103
+ if (reportFormat === false) {
104
+ if (getHeader(request.headers, 'x-application-profile')) {
105
+ return {
106
+ ...response,
107
+ headers: {
108
+ ...response.headers,
109
+ 'x-application-profile': JSON.stringify(profile.data())
110
+ }
111
+ };
112
+ }
113
+ return response;
114
+ // override response with html report
115
+ }
116
+ else if (reportFormat === 'html') {
117
+ return apiHtmlResponse(200, makeHtmlReport(profile.report()));
118
+ }
119
+ // override response with json report
120
+ return apiJsonResponse(200, profile.report());
121
+ });
122
+ }
123
+ return responsePromise;
124
+ };
125
+ export const makeHtmlReport = (report) => `
126
+ <head>
127
+ <style>
128
+ table {
129
+ border-collapse: collapse;
130
+ }
131
+ table td,th {
132
+ border: 1px solid #ccc;
133
+ padding: 10px;
134
+ }
135
+ summary {
136
+ padding: 10px 0;
137
+ }
138
+ </style>
139
+ </head>
140
+ <body>
141
+ ${makeHtmlCallReport(report, 0)}
142
+ </body>
143
+ `;
144
+ export const makeHtmlProfileReport = (report) => `
145
+ <details>
146
+ <summary>Profile: ${report.name}</summary>
147
+ <table>
148
+ <thead>
149
+ <tr>
150
+ <th>call #</th>
151
+ <th>children</th>
152
+ <th>time</th>
153
+ <th>time coverage (accounted/unaccounted/percent)</th>
154
+ </tr>
155
+ </thead>
156
+ <tbody>
157
+ ${report.calls.map((call, i) => `<tr>
158
+ <td>${i + 1}</td>
159
+ <td>${call.children.length}</td>
160
+ <td>${call.time}</td>
161
+ <td>${typeof call.coverage === 'string' ? call.coverage : `${call.coverage.accountedTime}/${call.coverage.unaccountedTime}/${call.coverage.accountedPercent}`}</td>
162
+ </tr>`)}
163
+ </tbody>
164
+ </table>
165
+ <div style="padding-left: 20px; border-left: 1px solid #ccc;">
166
+ ${report.calls.map(makeHtmlCallReport).join('')}
167
+ </div>
168
+ </details>
169
+ `;
170
+ export const makeHtmlCallReport = (report, index) => `
171
+ <h4>call # ${index + 1} children<h4>
172
+ <table>
173
+ <thead>
174
+ <tr>
175
+ <th>name</th>
176
+ <th>calls</th>
177
+ <th>time (total (min/max/avg))</th>
178
+ </tr>
179
+ </thead>
180
+ <tbody>
181
+ ${report.children.map(profile => `<tr>
182
+ <td>${profile.name}</td>
183
+ <td>${profile.calls.length}</td>
184
+ <td>${typeof profile.time === 'string' ? profile.time : `${profile.time.total} (${profile.time.min}/${profile.time.max}/${profile.time.avg})`}</td>
185
+ </tr>`).join('')}
186
+ </tbody>
187
+ </table>
188
+ <div style="margin-left: 20px">
189
+ ${report.children.map(makeHtmlProfileReport).join('')}
190
+ </div>
191
+ `;