@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,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.assignmentsGateway = void 0;
|
|
4
|
+
const zod_1 = require("zod");
|
|
5
|
+
const index_js_1 = require("../../config/index.js");
|
|
6
|
+
const index_js_2 = require("../../guards/index.js");
|
|
7
|
+
const helpers_js_1 = require("../../misc/helpers.js");
|
|
8
|
+
const index_js_3 = require("../../routing/index.js");
|
|
9
|
+
const emptySchema = zod_1.z.object({});
|
|
10
|
+
const assignmentsGateway = (initializer) => (configProvider) => {
|
|
11
|
+
const config = configProvider[(0, index_js_2.ifDefined)(initializer.configSpace, 'assignments')];
|
|
12
|
+
const assignmentsBase = (0, helpers_js_1.once)(async () => (await (0, index_js_1.resolveConfigValue)(config.assignmentsBase)).replace(/\/+$/, ''));
|
|
13
|
+
const request = async (path, body, expectedStatus, schema, extraHeaders) => {
|
|
14
|
+
const url = `${await assignmentsBase()}${path}`;
|
|
15
|
+
const bodyStr = JSON.stringify(body);
|
|
16
|
+
const signedHeaders = await initializer.httpMessageSigner.signRequest(index_js_3.METHOD.POST, url, bodyStr);
|
|
17
|
+
const response = await initializer.fetch(url, {
|
|
18
|
+
method: index_js_3.METHOD.POST,
|
|
19
|
+
headers: { 'Content-Type': 'application/json', ...extraHeaders, ...signedHeaders },
|
|
20
|
+
body: bodyStr,
|
|
21
|
+
});
|
|
22
|
+
if (response.status !== expectedStatus) {
|
|
23
|
+
throw new Error(`assignments returned ${response.status}: ${await response.text()}`);
|
|
24
|
+
}
|
|
25
|
+
return schema.parse(await response.json());
|
|
26
|
+
};
|
|
27
|
+
const completeAttempt = (launchToken, variant) => request('/api/v0/integrations/complete-attempt', { launchToken, ...(variant !== null && variant !== void 0 ? variant : {}) }, 201, emptySchema);
|
|
28
|
+
const scoreAttempt = (launchToken, score) => request('/api/v0/integrations/score-attempt', { launchToken, score }, 201, emptySchema);
|
|
29
|
+
return { completeAttempt, scoreAttempt };
|
|
30
|
+
};
|
|
31
|
+
exports.assignmentsGateway = assignmentsGateway;
|