@esushealth/sdk 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +146 -2
- package/dist/app-session.d.ts +225 -0
- package/dist/app-session.js +286 -0
- package/dist/clinical-workflows.d.ts +279 -0
- package/dist/clinical-workflows.js +43 -0
- package/dist/diagnostics.d.ts +24 -0
- package/dist/diagnostics.js +57 -0
- package/dist/fhir-contract.d.ts +12540 -0
- package/dist/fhir-contract.js +95 -0
- package/dist/fhir.d.ts +94 -0
- package/dist/fhir.js +169 -0
- package/dist/files.d.ts +13 -0
- package/dist/files.js +40 -0
- package/dist/integration-contract.d.ts +2327 -0
- package/dist/integration-contract.js +124 -0
- package/dist/integration-request.d.ts +11 -0
- package/dist/integration-request.js +19 -0
- package/dist/organization.d.ts +324 -0
- package/dist/organization.js +43 -0
- package/dist/patient-contract.d.ts +2 -0
- package/dist/patient-contract.js +1 -0
- package/dist/public.d.ts +125 -0
- package/dist/public.js +34 -0
- package/dist/server.d.ts +1310 -0
- package/dist/server.js +85 -0
- package/dist/smart.d.ts +42 -0
- package/dist/smart.js +98 -0
- package/dist/tenant-apps.d.ts +355 -0
- package/dist/tenant-apps.js +50 -0
- package/dist/transport.d.ts +38 -0
- package/dist/transport.js +108 -0
- package/dist/webhook-management.d.ts +53 -0
- package/dist/webhook-management.js +83 -0
- package/dist/webhooks.d.ts +24 -0
- package/dist/webhooks.js +41 -0
- package/package.json +17 -4
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
// Generated from the registered Esus API FHIR routes. Do not edit.
|
|
2
|
+
export const fhirResources = [
|
|
3
|
+
"Account",
|
|
4
|
+
"ActivityDefinition",
|
|
5
|
+
"AdverseEvent",
|
|
6
|
+
"AllergyIntolerance",
|
|
7
|
+
"Appointment",
|
|
8
|
+
"AppointmentResponse",
|
|
9
|
+
"CarePlan",
|
|
10
|
+
"CareTeam",
|
|
11
|
+
"Claim",
|
|
12
|
+
"ClaimResponse",
|
|
13
|
+
"ClinicalImpression",
|
|
14
|
+
"CodeSystem",
|
|
15
|
+
"Communication",
|
|
16
|
+
"Composition",
|
|
17
|
+
"Condition",
|
|
18
|
+
"Consent",
|
|
19
|
+
"Coverage",
|
|
20
|
+
"DetectedIssue",
|
|
21
|
+
"Device",
|
|
22
|
+
"DiagnosticReport",
|
|
23
|
+
"DocumentReference",
|
|
24
|
+
"Encounter",
|
|
25
|
+
"EpisodeOfCare",
|
|
26
|
+
"ExplanationOfBenefit",
|
|
27
|
+
"FamilyMemberHistory",
|
|
28
|
+
"Flag",
|
|
29
|
+
"Goal",
|
|
30
|
+
"Group",
|
|
31
|
+
"HealthcareService",
|
|
32
|
+
"Immunization",
|
|
33
|
+
"ImmunizationEvaluation",
|
|
34
|
+
"ImmunizationRecommendation",
|
|
35
|
+
"Invoice",
|
|
36
|
+
"List",
|
|
37
|
+
"Location",
|
|
38
|
+
"Measure",
|
|
39
|
+
"MeasureReport",
|
|
40
|
+
"Media",
|
|
41
|
+
"Medication",
|
|
42
|
+
"MedicationAdministration",
|
|
43
|
+
"MedicationDispense",
|
|
44
|
+
"MedicationKnowledge",
|
|
45
|
+
"MedicationRequest",
|
|
46
|
+
"MedicationStatement",
|
|
47
|
+
"NutritionOrder",
|
|
48
|
+
"Observation",
|
|
49
|
+
"Organization",
|
|
50
|
+
"Patient",
|
|
51
|
+
"PlanDefinition",
|
|
52
|
+
"Practitioner",
|
|
53
|
+
"PractitionerRole",
|
|
54
|
+
"Procedure",
|
|
55
|
+
"Provenance",
|
|
56
|
+
"Questionnaire",
|
|
57
|
+
"QuestionnaireResponse",
|
|
58
|
+
"RelatedPerson",
|
|
59
|
+
"RiskAssessment",
|
|
60
|
+
"Schedule",
|
|
61
|
+
"ServiceRequest",
|
|
62
|
+
"Slot",
|
|
63
|
+
"Specimen",
|
|
64
|
+
"Task",
|
|
65
|
+
"ValueSet",
|
|
66
|
+
"VisionPrescription"
|
|
67
|
+
];
|
|
68
|
+
export const fhirLifecycleRestrictions = {
|
|
69
|
+
Organization: ["create", "delete"],
|
|
70
|
+
};
|
|
71
|
+
export const fhirOperations = {
|
|
72
|
+
"DELETE /bulk-export/:id": { "mode": "empty", "status": [204] },
|
|
73
|
+
"DELETE /fhir/Practitioner/roles/:roleId": { "mode": "empty", "status": [204] },
|
|
74
|
+
"GET /bulk-export/:id": { "mode": "json", "status": [200, 202] },
|
|
75
|
+
"GET /bulk-export/:id/download/:type": { "mode": "stream", "status": [200] },
|
|
76
|
+
"GET /fhir/$export": { "mode": "empty", "status": [202] },
|
|
77
|
+
"GET /fhir/CodeSystem/$lookup": { "mode": "json", "status": [200] },
|
|
78
|
+
"GET /fhir/Encounter/:id/$everything": { "mode": "json", "status": [200] },
|
|
79
|
+
"GET /fhir/Location/:id/$hierarchy": { "mode": "json", "status": [200] },
|
|
80
|
+
"GET /fhir/metadata": { "mode": "json", "status": [200] },
|
|
81
|
+
"GET /fhir/Patient/:id/$everything": { "mode": "json", "status": [200] },
|
|
82
|
+
"GET /fhir/Patient/$export": { "mode": "empty", "status": [202] },
|
|
83
|
+
"GET /fhir/Practitioner/:id/roles": { "mode": "json", "status": [200] },
|
|
84
|
+
"GET /fhir/ValueSet/:id/$expand": { "mode": "json", "status": [200] },
|
|
85
|
+
"POST /fhir/": { "mode": "json", "status": [200] },
|
|
86
|
+
"POST /fhir/$validate": { "mode": "json", "status": [200] },
|
|
87
|
+
"POST /fhir/Appointment/$book": { "mode": "json", "status": [201] },
|
|
88
|
+
"POST /fhir/Encounter/:id/$discharge": { "mode": "json", "status": [200] },
|
|
89
|
+
"POST /fhir/Encounter/:id/$transfer": { "mode": "json", "status": [200] },
|
|
90
|
+
"POST /fhir/Encounter/$admit": { "mode": "json", "status": [201] },
|
|
91
|
+
"POST /fhir/Practitioner/:id/roles": { "mode": "json", "status": [201] },
|
|
92
|
+
};
|
|
93
|
+
export const smartFhirOperations = [
|
|
94
|
+
"GET /fhir/Patient/:id/$everything",
|
|
95
|
+
];
|
package/dist/fhir.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { fhirLifecycleRestrictions, type FhirCreateInputs, type FhirOperation, type FhirOperationInputs, type FhirOperationResults, type FhirResourceFields, type FhirResourceType, type FhirUpdateInputs, type ResourceSearchFields } from "./fhir-contract.js";
|
|
2
|
+
import type { PatientInput } from "./patient-contract.js";
|
|
3
|
+
import { type RequestOptions, type Transport } from "./transport.js";
|
|
4
|
+
type ResourceIdentity<R extends FhirResourceType> = Readonly<{
|
|
5
|
+
resourceType: R;
|
|
6
|
+
id: string;
|
|
7
|
+
meta?: Readonly<{
|
|
8
|
+
versionId?: string;
|
|
9
|
+
lastUpdated?: string;
|
|
10
|
+
}>;
|
|
11
|
+
}>;
|
|
12
|
+
export type Patient = ResourceIdentity<"Patient"> & Omit<PatientInput, "managingOrganizationId"> & Readonly<{
|
|
13
|
+
managingOrganization?: Readonly<{
|
|
14
|
+
reference?: string;
|
|
15
|
+
display?: string;
|
|
16
|
+
}>;
|
|
17
|
+
}>;
|
|
18
|
+
export type PatientUpdateInput = FhirUpdateInputs["Patient"];
|
|
19
|
+
export type FhirResource<R extends FhirResourceType> = R extends "Patient" ? Patient : ResourceIdentity<R> & FhirResourceFields[R] & Readonly<Record<string, unknown>>;
|
|
20
|
+
type Pagination = Readonly<{
|
|
21
|
+
_count?: number;
|
|
22
|
+
_offset?: number;
|
|
23
|
+
}>;
|
|
24
|
+
export type FhirSearchFilters<R extends FhirResourceType> = ResourceSearchFields[R] & Pagination & Readonly<{
|
|
25
|
+
_id?: string;
|
|
26
|
+
_lastUpdated?: string | readonly string[];
|
|
27
|
+
_sort?: string;
|
|
28
|
+
_elements?: string;
|
|
29
|
+
_summary?: "true" | "text" | "data" | "count";
|
|
30
|
+
_total?: "none" | "estimate" | "accurate";
|
|
31
|
+
}>;
|
|
32
|
+
export type PatientFilters = FhirSearchFilters<"Patient">;
|
|
33
|
+
export type JsonPatchOperation = Readonly<{
|
|
34
|
+
path: string;
|
|
35
|
+
}> & (Readonly<{
|
|
36
|
+
op: "add" | "replace" | "test";
|
|
37
|
+
value: unknown;
|
|
38
|
+
}> | Readonly<{
|
|
39
|
+
op: "remove";
|
|
40
|
+
}> | Readonly<{
|
|
41
|
+
op: "move" | "copy";
|
|
42
|
+
from: string;
|
|
43
|
+
}>);
|
|
44
|
+
type LifecycleAction = "create" | "delete";
|
|
45
|
+
type Restrictions = typeof fhirLifecycleRestrictions;
|
|
46
|
+
type WritableResource<A extends LifecycleAction> = Exclude<FhirResourceType, {
|
|
47
|
+
[R in keyof Restrictions]: A extends Restrictions[R][number] ? R : never;
|
|
48
|
+
}[keyof Restrictions]>;
|
|
49
|
+
export declare function supportsFhirLifecycle<A extends LifecycleAction>(resource: FhirResourceType, action: A): resource is WritableResource<A>;
|
|
50
|
+
export declare function isFhirResource(resource: string): resource is FhirResourceType;
|
|
51
|
+
export declare function readResource<R extends FhirResourceType>(expected: R, body: unknown, response: Response): FhirResource<R>;
|
|
52
|
+
export declare function validSegment(value: unknown): value is string;
|
|
53
|
+
export declare function fhirOperation<K extends FhirOperation = FhirOperation>(request: Transport, allowed?: readonly K[]): <O extends K>(operation: O, input: FhirOperationInputs[NoInfer<O>], options?: RequestOptions) => Promise<FhirOperationResults[O]>;
|
|
54
|
+
export declare function queryPath(path: string, fields: object): string;
|
|
55
|
+
type ReadResult<R extends FhirResourceType> = Readonly<{
|
|
56
|
+
etag: string | null;
|
|
57
|
+
}> & (R extends "Patient" ? {
|
|
58
|
+
patient: Patient;
|
|
59
|
+
} : {
|
|
60
|
+
resource: FhirResource<R>;
|
|
61
|
+
});
|
|
62
|
+
export declare function readSearchResult<R extends FhirResourceType>(resource: R, body: unknown, response: Response): {
|
|
63
|
+
resources: FhirResource<R>[];
|
|
64
|
+
total: number | undefined;
|
|
65
|
+
};
|
|
66
|
+
export declare function readFhir(request: Transport): {
|
|
67
|
+
search: <R extends FhirResourceType>(resource: R, filters?: FhirSearchFilters<NoInfer<R>>, options?: RequestOptions) => Promise<{
|
|
68
|
+
resources: FhirResource<R>[];
|
|
69
|
+
total: number | undefined;
|
|
70
|
+
}>;
|
|
71
|
+
list(resource: "Patient", filters?: PatientFilters, options?: RequestOptions): Promise<{
|
|
72
|
+
patients: Patient[];
|
|
73
|
+
total: number | undefined;
|
|
74
|
+
}>;
|
|
75
|
+
read: <R extends FhirResourceType>(resource: R, id: string, options?: RequestOptions) => Promise<ReadResult<R>>;
|
|
76
|
+
history<R extends FhirResourceType>(resource: R, id: string, page?: Pagination, options?: RequestOptions): Promise<{
|
|
77
|
+
entries: {
|
|
78
|
+
resource: FhirResource<R>;
|
|
79
|
+
}[];
|
|
80
|
+
total: number | undefined;
|
|
81
|
+
}>;
|
|
82
|
+
readVersion<R extends FhirResourceType>(resource: R, id: string, version: string, options?: RequestOptions): Promise<{
|
|
83
|
+
resource: FhirResource<R>;
|
|
84
|
+
etag: string | null;
|
|
85
|
+
}>;
|
|
86
|
+
};
|
|
87
|
+
export declare function writeFhir(request: Transport): {
|
|
88
|
+
create<R extends WritableResource<"create">>(resource: R, input: FhirCreateInputs[NoInfer<R>], options?: RequestOptions): Promise<FhirResource<R>>;
|
|
89
|
+
update<R extends FhirResourceType>(resource: R, id: string, input: FhirUpdateInputs[NoInfer<R>], etag: string, options?: RequestOptions): Promise<FhirResource<R>>;
|
|
90
|
+
patch<R extends FhirResourceType>(resource: R, id: string, input: FhirUpdateInputs[NoInfer<R>], etag: string, options?: RequestOptions): Promise<FhirResource<R>>;
|
|
91
|
+
patchJson<R extends FhirResourceType>(resource: R, id: string, input: readonly JsonPatchOperation[], etag: string, options?: RequestOptions): Promise<FhirResource<R>>;
|
|
92
|
+
delete(resource: WritableResource<"delete">, id: string, options?: RequestOptions): Promise<void>;
|
|
93
|
+
};
|
|
94
|
+
export {};
|
package/dist/fhir.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { fhirLifecycleRestrictions, fhirOperations, fhirResources, } from "./fhir-contract.js";
|
|
2
|
+
import { EsusError, record } from "./transport.js";
|
|
3
|
+
const resourceNames = new Set(fhirResources);
|
|
4
|
+
export function supportsFhirLifecycle(resource, action) {
|
|
5
|
+
const restrictions = fhirLifecycleRestrictions;
|
|
6
|
+
return !restrictions[resource]?.includes(action);
|
|
7
|
+
}
|
|
8
|
+
export function isFhirResource(resource) {
|
|
9
|
+
return resourceNames.has(resource);
|
|
10
|
+
}
|
|
11
|
+
function invalidResponse(response, message) {
|
|
12
|
+
throw new EsusError(message, response.status, response.headers.get("x-request-id"));
|
|
13
|
+
}
|
|
14
|
+
export function readResource(expected, body, response) {
|
|
15
|
+
const resource = record(body);
|
|
16
|
+
if (resource?.resourceType !== expected || typeof resource.id !== "string") {
|
|
17
|
+
return invalidResponse(response, `Esus did not return a ${expected} resource`);
|
|
18
|
+
}
|
|
19
|
+
return resource;
|
|
20
|
+
}
|
|
21
|
+
export function validSegment(value) {
|
|
22
|
+
return typeof value === "string" && /^[A-Za-z0-9.-]{1,64}$/.test(value) && value !== "." && value !== "..";
|
|
23
|
+
}
|
|
24
|
+
function resourcePath(resource, id) {
|
|
25
|
+
if (!isFhirResource(resource))
|
|
26
|
+
throw new TypeError("This SDK only supports resources in the Esus FHIR catalog");
|
|
27
|
+
if (id === undefined)
|
|
28
|
+
return `/fhir/${resource}`;
|
|
29
|
+
if (!validSegment(id)) {
|
|
30
|
+
throw new TypeError(`Invalid ${resource} id`);
|
|
31
|
+
}
|
|
32
|
+
return `/fhir/${resource}/${id}`;
|
|
33
|
+
}
|
|
34
|
+
const operationNames = Object.keys(fhirOperations);
|
|
35
|
+
export function fhirOperation(request, allowed) {
|
|
36
|
+
const allowedNames = new Set(allowed ?? operationNames);
|
|
37
|
+
return async (operation, input, options = {}) => {
|
|
38
|
+
if (!allowedNames.has(operation))
|
|
39
|
+
throw new TypeError("This FHIR operation is unavailable for this client");
|
|
40
|
+
const { mode, status } = fhirOperations[operation];
|
|
41
|
+
const data = input;
|
|
42
|
+
const separator = operation.indexOf(" ");
|
|
43
|
+
const path = operation.slice(separator + 1).replace(/:([A-Za-z]+)/g, (_match, name) => {
|
|
44
|
+
const value = data.params?.[name];
|
|
45
|
+
if (!validSegment(value))
|
|
46
|
+
throw new TypeError(`Invalid FHIR operation parameter: ${name}`);
|
|
47
|
+
return value;
|
|
48
|
+
});
|
|
49
|
+
const { body, response } = await request(data.query ? queryPath(path, data.query) : path, {
|
|
50
|
+
...options, method: operation.slice(0, separator), responseMode: mode,
|
|
51
|
+
...(data.body === undefined ? {} : {
|
|
52
|
+
headers: { "content-type": "application/fhir+json" }, body: JSON.stringify(data.body),
|
|
53
|
+
}),
|
|
54
|
+
});
|
|
55
|
+
if (!status.includes(response.status)) {
|
|
56
|
+
return invalidResponse(response, "Esus returned an unexpected FHIR operation status");
|
|
57
|
+
}
|
|
58
|
+
return { status: response.status, body, headers: response.headers };
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
export function queryPath(path, fields) {
|
|
62
|
+
const query = new URLSearchParams();
|
|
63
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
64
|
+
if (value === undefined)
|
|
65
|
+
continue;
|
|
66
|
+
for (const item of Array.isArray(value) ? value : [value])
|
|
67
|
+
query.append(key, String(item));
|
|
68
|
+
}
|
|
69
|
+
return `${path}?${query}`;
|
|
70
|
+
}
|
|
71
|
+
function readBundle(body, response, type) {
|
|
72
|
+
const bundle = record(body);
|
|
73
|
+
if (bundle?.resourceType !== "Bundle" || bundle.type !== type) {
|
|
74
|
+
return invalidResponse(response, `Esus did not return a ${type} bundle`);
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
entries: Array.isArray(bundle.entry) ? bundle.entry : [],
|
|
78
|
+
total: typeof bundle.total === "number" ? bundle.total : undefined,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
export function readSearchResult(resource, body, response) {
|
|
82
|
+
const { entries, total } = readBundle(body, response, "searchset");
|
|
83
|
+
const resources = entries.flatMap((entry) => {
|
|
84
|
+
const row = record(entry);
|
|
85
|
+
const item = record(row?.resource);
|
|
86
|
+
return item?.resourceType === resource && record(row?.search)?.mode !== "include"
|
|
87
|
+
? [readResource(resource, item, response)]
|
|
88
|
+
: [];
|
|
89
|
+
});
|
|
90
|
+
return { resources, total };
|
|
91
|
+
}
|
|
92
|
+
export function readFhir(request) {
|
|
93
|
+
async function search(resource, filters, options = {}) {
|
|
94
|
+
const { body, response } = await request(queryPath(resourcePath(resource), filters ?? {}), options);
|
|
95
|
+
return readSearchResult(resource, body, response);
|
|
96
|
+
}
|
|
97
|
+
async function read(resource, id, options = {}) {
|
|
98
|
+
const { body, response } = await request(resourcePath(resource, id), options);
|
|
99
|
+
const value = readResource(resource, body, response);
|
|
100
|
+
const etag = response.headers.get("etag");
|
|
101
|
+
return value.resourceType === "Patient" ? { patient: value, etag } : { resource: value, etag };
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
search,
|
|
105
|
+
async list(resource, filters, options) {
|
|
106
|
+
const { resources, total } = await search(resource, filters, options);
|
|
107
|
+
return { patients: resources, total };
|
|
108
|
+
},
|
|
109
|
+
read,
|
|
110
|
+
async history(resource, id, page = {}, options = {}) {
|
|
111
|
+
const path = queryPath(`${resourcePath(resource, id)}/_history`, page);
|
|
112
|
+
const { body, response } = await request(path, options);
|
|
113
|
+
const { entries, total } = readBundle(body, response, "history");
|
|
114
|
+
return {
|
|
115
|
+
entries: entries.map((entry) => {
|
|
116
|
+
const row = record(entry);
|
|
117
|
+
return { ...row, resource: readResource(resource, row?.resource, response) };
|
|
118
|
+
}),
|
|
119
|
+
total,
|
|
120
|
+
};
|
|
121
|
+
},
|
|
122
|
+
async readVersion(resource, id, version, options = {}) {
|
|
123
|
+
if (!/^[1-9][0-9]*$/.test(version))
|
|
124
|
+
throw new TypeError("Invalid FHIR version");
|
|
125
|
+
const { body, response } = await request(`${resourcePath(resource, id)}/_history/${version}`, options);
|
|
126
|
+
return { resource: readResource(resource, body, response), etag: response.headers.get("etag") };
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
export function writeFhir(request) {
|
|
131
|
+
const json = "application/fhir+json";
|
|
132
|
+
async function write(resource, path, init) {
|
|
133
|
+
const { body, response } = await request(path, init);
|
|
134
|
+
return readResource(resource, body, response);
|
|
135
|
+
}
|
|
136
|
+
return {
|
|
137
|
+
create(resource, input, options = {}) {
|
|
138
|
+
if (!supportsFhirLifecycle(resource, "create"))
|
|
139
|
+
throw new TypeError(`FHIR create is unavailable for ${resource}`);
|
|
140
|
+
return write(resource, resourcePath(resource), {
|
|
141
|
+
...options, method: "POST", headers: { "content-type": json }, body: JSON.stringify(input),
|
|
142
|
+
});
|
|
143
|
+
},
|
|
144
|
+
update(resource, id, input, etag, options = {}) {
|
|
145
|
+
return write(resource, resourcePath(resource, id), {
|
|
146
|
+
...options, method: "PUT", headers: { "content-type": json, "if-match": etag }, body: JSON.stringify(input),
|
|
147
|
+
});
|
|
148
|
+
},
|
|
149
|
+
patch(resource, id, input, etag, options = {}) {
|
|
150
|
+
return write(resource, resourcePath(resource, id), {
|
|
151
|
+
...options, method: "PATCH", headers: { "content-type": "application/merge-patch+json", "if-match": etag },
|
|
152
|
+
body: JSON.stringify(input),
|
|
153
|
+
});
|
|
154
|
+
},
|
|
155
|
+
patchJson(resource, id, input, etag, options = {}) {
|
|
156
|
+
return write(resource, resourcePath(resource, id), {
|
|
157
|
+
...options, method: "PATCH", headers: { "content-type": "application/json-patch+json", "if-match": etag },
|
|
158
|
+
body: JSON.stringify(input),
|
|
159
|
+
});
|
|
160
|
+
},
|
|
161
|
+
async delete(resource, id, options = {}) {
|
|
162
|
+
if (!supportsFhirLifecycle(resource, "delete"))
|
|
163
|
+
throw new TypeError(`FHIR delete is unavailable for ${resource}`);
|
|
164
|
+
const { response } = await request(resourcePath(resource, id), { ...options, method: "DELETE" });
|
|
165
|
+
if (response.status !== 204)
|
|
166
|
+
invalidResponse(response, "Esus did not confirm deletion");
|
|
167
|
+
},
|
|
168
|
+
};
|
|
169
|
+
}
|
package/dist/files.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { IntegrationInputs, IntegrationResults } from "./integration-contract.js";
|
|
2
|
+
import { type RequestOptions, type Transport } from "./transport.js";
|
|
3
|
+
export type UploadedFile = IntegrationResults["POST /files/upload"]["body"];
|
|
4
|
+
type Upload = IntegrationInputs["POST /files/upload"]["file"];
|
|
5
|
+
export declare function filesClient(request: Transport): {
|
|
6
|
+
upload(file: Upload, { signal }?: RequestOptions): Promise<UploadedFile>;
|
|
7
|
+
download(id: string, { signal }?: RequestOptions): Promise<{
|
|
8
|
+
body: ReadableStream<any>;
|
|
9
|
+
headers: Headers;
|
|
10
|
+
}>;
|
|
11
|
+
delete(id: string, { signal }?: RequestOptions): Promise<void>;
|
|
12
|
+
};
|
|
13
|
+
export {};
|
package/dist/files.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { EsusError, record } from "./transport.js";
|
|
2
|
+
const uuid = /^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$/i;
|
|
3
|
+
function filePath(id) {
|
|
4
|
+
if (!uuid.test(id))
|
|
5
|
+
throw new TypeError("Invalid Esus file ID");
|
|
6
|
+
return `/files/${id.toLowerCase()}`;
|
|
7
|
+
}
|
|
8
|
+
function invalid(response) {
|
|
9
|
+
throw new EsusError("Esus returned an invalid file response", response.status, response.headers.get("x-request-id"));
|
|
10
|
+
}
|
|
11
|
+
export function filesClient(request) {
|
|
12
|
+
return {
|
|
13
|
+
async upload(file, { signal } = {}) {
|
|
14
|
+
const form = new FormData();
|
|
15
|
+
form.set("file", file);
|
|
16
|
+
const { body, response } = await request("/files/upload", { method: "POST", signal, body: form });
|
|
17
|
+
const uploaded = record(body);
|
|
18
|
+
if (response.status !== 200 || typeof uploaded?.id !== "string" || !uuid.test(uploaded.id) ||
|
|
19
|
+
uploaded.url !== filePath(uploaded.id) || typeof uploaded.key !== "string" ||
|
|
20
|
+
typeof uploaded.contentType !== "string" || typeof uploaded.size !== "number")
|
|
21
|
+
return invalid(response);
|
|
22
|
+
return uploaded;
|
|
23
|
+
},
|
|
24
|
+
async download(id, { signal } = {}) {
|
|
25
|
+
const { body, response } = await request(filePath(id), { signal, responseMode: "binary" });
|
|
26
|
+
const { headers } = response;
|
|
27
|
+
if (response.status !== 200 || !(body instanceof ReadableStream) ||
|
|
28
|
+
!/^attachment(?:;|$)/i.test(headers.get("content-disposition") ?? "") || !headers.get("content-type")) {
|
|
29
|
+
void response.body?.cancel().catch(() => { });
|
|
30
|
+
return invalid(response);
|
|
31
|
+
}
|
|
32
|
+
return { body, headers };
|
|
33
|
+
},
|
|
34
|
+
async delete(id, { signal } = {}) {
|
|
35
|
+
const { body, response } = await request(filePath(id), { method: "DELETE", signal });
|
|
36
|
+
if (response.status !== 200 || record(body)?.success !== true)
|
|
37
|
+
invalid(response);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|