@openstax/ts-utils 1.46.0 → 1.47.0
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/assignmentsGateway/index.d.ts +26 -0
- package/dist/cjs/services/assignmentsGateway/index.js +31 -0
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/assignmentsGateway/index.d.ts +26 -0
- package/dist/esm/services/assignmentsGateway/index.js +27 -0
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ConfigProviderForConfig } from '../../config/index.js';
|
|
2
|
+
import { GenericFetch } from '../../fetch/index.js';
|
|
3
|
+
import { HttpMessageSigner } from '../httpMessage/signer.js';
|
|
4
|
+
export type Config = {
|
|
5
|
+
assignmentsBase: string;
|
|
6
|
+
};
|
|
7
|
+
export type RawMinMaxScore = {
|
|
8
|
+
raw: number;
|
|
9
|
+
min: number;
|
|
10
|
+
max: number;
|
|
11
|
+
};
|
|
12
|
+
interface Initializer<C> {
|
|
13
|
+
configSpace?: C;
|
|
14
|
+
fetch: GenericFetch;
|
|
15
|
+
httpMessageSigner: HttpMessageSigner;
|
|
16
|
+
}
|
|
17
|
+
export declare const assignmentsGateway: <C extends string = "assignments">(initializer: Initializer<C>) => (configProvider: { [_key in C]: ConfigProviderForConfig<Config>; }) => {
|
|
18
|
+
completeAttempt: (launchToken: string, variant?: {
|
|
19
|
+
score: RawMinMaxScore;
|
|
20
|
+
} | {
|
|
21
|
+
pendingScore: true;
|
|
22
|
+
}) => Promise<Record<string, never>>;
|
|
23
|
+
scoreAttempt: (launchToken: string, score: RawMinMaxScore) => Promise<Record<string, never>>;
|
|
24
|
+
};
|
|
25
|
+
export type AssignmentsGateway = ReturnType<ReturnType<typeof assignmentsGateway>>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { resolveConfigValue } from '../../config/index.js';
|
|
3
|
+
import { ifDefined } from '../../guards/index.js';
|
|
4
|
+
import { once } from '../../misc/helpers.js';
|
|
5
|
+
import { METHOD } from '../../routing/index.js';
|
|
6
|
+
const emptySchema = z.object({});
|
|
7
|
+
export const assignmentsGateway = (initializer) => (configProvider) => {
|
|
8
|
+
const config = configProvider[ifDefined(initializer.configSpace, 'assignments')];
|
|
9
|
+
const assignmentsBase = once(async () => (await resolveConfigValue(config.assignmentsBase)).replace(/\/+$/, ''));
|
|
10
|
+
const request = async (path, body, expectedStatus, schema, extraHeaders) => {
|
|
11
|
+
const url = `${await assignmentsBase()}${path}`;
|
|
12
|
+
const bodyStr = JSON.stringify(body);
|
|
13
|
+
const signedHeaders = await initializer.httpMessageSigner.signRequest(METHOD.POST, url, bodyStr);
|
|
14
|
+
const response = await initializer.fetch(url, {
|
|
15
|
+
method: METHOD.POST,
|
|
16
|
+
headers: { 'Content-Type': 'application/json', ...extraHeaders, ...signedHeaders },
|
|
17
|
+
body: bodyStr,
|
|
18
|
+
});
|
|
19
|
+
if (response.status !== expectedStatus) {
|
|
20
|
+
throw new Error(`assignments returned ${response.status}: ${await response.text()}`);
|
|
21
|
+
}
|
|
22
|
+
return schema.parse(await response.json());
|
|
23
|
+
};
|
|
24
|
+
const completeAttempt = (launchToken, variant) => request('/api/v0/integrations/complete-attempt', { launchToken, ...(variant !== null && variant !== void 0 ? variant : {}) }, 201, emptySchema);
|
|
25
|
+
const scoreAttempt = (launchToken, score) => request('/api/v0/integrations/score-attempt', { launchToken, score }, 201, emptySchema);
|
|
26
|
+
return { completeAttempt, scoreAttempt };
|
|
27
|
+
};
|