Skip to content

Commit 13a6f6b

Browse files
authored
Add attachments resources to esm-api and esm-react-utils (supporting O3-2618) (#860)
* Add attachments resources to esm-api and esm-react-utils (supporting O3-2618) * Attachments API docs * Add attachments mocks
1 parent 358813b commit 13a6f6b

File tree

13 files changed

+465
-2
lines changed

13 files changed

+465
-2
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/** @module @category API */
2+
import type { UploadedFile } from './types';
3+
import { openmrsFetch } from './openmrs-fetch';
4+
5+
export const attachmentUrl = '/ws/rest/v1/attachment';
6+
7+
export function getAttachmentByUuid(attachmentUuid: string, abortController: AbortController) {
8+
return openmrsFetch(`${attachmentUrl}/${attachmentUuid}`, {
9+
signal: abortController.signal,
10+
});
11+
}
12+
13+
export function getAttachments(patientUuid: string, includeEncounterless: boolean, abortController: AbortController) {
14+
return openmrsFetch(`${attachmentUrl}?patient=${patientUuid}&includeEncounterless=${includeEncounterless}`, {
15+
signal: abortController.signal,
16+
});
17+
}
18+
19+
export async function createAttachment(patientUuid: string, fileToUpload: UploadedFile) {
20+
const formData = new FormData();
21+
22+
formData.append('fileCaption', fileToUpload.fileName);
23+
formData.append('patient', patientUuid);
24+
25+
if (fileToUpload.file) {
26+
formData.append('file', fileToUpload.file);
27+
} else {
28+
formData.append('file', new File([''], fileToUpload.fileName), fileToUpload.fileName);
29+
formData.append('base64Content', fileToUpload.base64Content);
30+
}
31+
return openmrsFetch(`${attachmentUrl}`, {
32+
method: 'POST',
33+
body: formData,
34+
});
35+
}
36+
37+
export function deleteAttachmentPermanently(attachmentUuid: string, abortController: AbortController) {
38+
return openmrsFetch(`${attachmentUrl}/${attachmentUuid}`, {
39+
method: 'DELETE',
40+
signal: abortController.signal,
41+
});
42+
}

packages/framework/esm-api/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './types';
2+
export * from './attachments';
23
export * from './openmrs-fetch';
34
export * from './setup';
45

packages/framework/esm-api/src/public.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './types';
22
export * from './openmrs-fetch';
3+
export * from './attachments';
34

45
export * from './shared-api-objects/current-user';
56
export * from './shared-api-objects/current-patient';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export interface UploadedFile {
2+
file?: File;
3+
base64Content: string;
4+
fileName: string;
5+
fileType: string;
6+
fileDescription: string;
7+
status?: 'uploading' | 'complete';
8+
}
9+
10+
export interface Attachment {
11+
id: string;
12+
src: string;
13+
title: string;
14+
description: string;
15+
dateTime: string;
16+
bytesMimeType: string;
17+
bytesContentFamily: string;
18+
}
19+
export interface AttachmentResponse {
20+
bytesContentFamily: string;
21+
bytesMimeType: string;
22+
comment: string;
23+
dateTime: string;
24+
uuid: string;
25+
}

packages/framework/esm-api/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './attachments-types';
12
export * from './fetch';
23
export * from './fhir-resource';
34
export * from './openmrs-resource';

packages/framework/esm-framework/docs/API.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
### API Functions
88

99
- [clearCurrentUser](API.md#clearcurrentuser)
10+
- [createAttachment](API.md#createattachment)
11+
- [deleteAttachmentPermanently](API.md#deleteattachmentpermanently)
1012
- [fetchCurrentPatient](API.md#fetchcurrentpatient)
13+
- [getAttachmentByUuid](API.md#getattachmentbyuuid)
14+
- [getAttachments](API.md#getattachments)
1115
- [getCurrentUser](API.md#getcurrentuser)
1216
- [getLocations](API.md#getlocations)
1317
- [getLoggedInUser](API.md#getloggedinuser)
@@ -159,6 +163,7 @@
159163
### Other Functions
160164

161165
- [ExtensionSlot](API.md#extensionslot)
166+
- [useAttachments](API.md#useattachments)
162167

163168
### Store Functions
164169

@@ -737,6 +742,16 @@ ___
737742

738743
___
739744

745+
### attachmentUrl
746+
747+
`Const` **attachmentUrl**: ``"/ws/rest/v1/attachment"``
748+
749+
#### Defined in
750+
751+
[packages/framework/esm-api/src/attachments.ts:5](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L5)
752+
753+
___
754+
740755
### defaultVisitCustomRepresentation
741756

742757
`Const` **defaultVisitCustomRepresentation**: `string`
@@ -935,6 +950,48 @@ ___
935950

936951
___
937952

953+
### createAttachment
954+
955+
**createAttachment**(`patientUuid`, `fileToUpload`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>
956+
957+
#### Parameters
958+
959+
| Name | Type |
960+
| :------ | :------ |
961+
| `patientUuid` | `string` |
962+
| `fileToUpload` | [`UploadedFile`](interfaces/UploadedFile.md) |
963+
964+
#### Returns
965+
966+
`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>
967+
968+
#### Defined in
969+
970+
[packages/framework/esm-api/src/attachments.ts:19](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L19)
971+
972+
___
973+
974+
### deleteAttachmentPermanently
975+
976+
**deleteAttachmentPermanently**(`attachmentUuid`, `abortController`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>
977+
978+
#### Parameters
979+
980+
| Name | Type |
981+
| :------ | :------ |
982+
| `attachmentUuid` | `string` |
983+
| `abortController` | `AbortController` |
984+
985+
#### Returns
986+
987+
`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>
988+
989+
#### Defined in
990+
991+
[packages/framework/esm-api/src/attachments.ts:37](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L37)
992+
993+
___
994+
938995
### fetchCurrentPatient
939996

940997
**fetchCurrentPatient**(`patientUuid`, `fetchInit?`, `includeOfflinePatients?`): `Promise`<`fhir.Patient` \| ``null``\>
@@ -957,6 +1014,49 @@ ___
9571014

9581015
___
9591016

1017+
### getAttachmentByUuid
1018+
1019+
**getAttachmentByUuid**(`attachmentUuid`, `abortController`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>
1020+
1021+
#### Parameters
1022+
1023+
| Name | Type |
1024+
| :------ | :------ |
1025+
| `attachmentUuid` | `string` |
1026+
| `abortController` | `AbortController` |
1027+
1028+
#### Returns
1029+
1030+
`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>
1031+
1032+
#### Defined in
1033+
1034+
[packages/framework/esm-api/src/attachments.ts:7](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L7)
1035+
1036+
___
1037+
1038+
### getAttachments
1039+
1040+
**getAttachments**(`patientUuid`, `includeEncounterless`, `abortController`): `Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>
1041+
1042+
#### Parameters
1043+
1044+
| Name | Type |
1045+
| :------ | :------ |
1046+
| `patientUuid` | `string` |
1047+
| `includeEncounterless` | `boolean` |
1048+
| `abortController` | `AbortController` |
1049+
1050+
#### Returns
1051+
1052+
`Promise`<[`FetchResponse`](interfaces/FetchResponse.md)<`any`\>\>
1053+
1054+
#### Defined in
1055+
1056+
[packages/framework/esm-api/src/attachments.ts:13](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/attachments.ts#L13)
1057+
1058+
___
1059+
9601060
### getCurrentUser
9611061

9621062
**getCurrentUser**(): `Observable`<[`Session`](interfaces/Session.md)\>
@@ -3963,6 +4063,35 @@ Passing a function as children
39634063

39644064
___
39654065

4066+
### useAttachments
4067+
4068+
**useAttachments**(`patientUuid`, `includeEncounterless`): `Object`
4069+
4070+
#### Parameters
4071+
4072+
| Name | Type |
4073+
| :------ | :------ |
4074+
| `patientUuid` | `string` |
4075+
| `includeEncounterless` | `boolean` |
4076+
4077+
#### Returns
4078+
4079+
`Object`
4080+
4081+
| Name | Type |
4082+
| :------ | :------ |
4083+
| `data` | [`AttachmentResponse`](interfaces/AttachmentResponse.md)[] |
4084+
| `error` | `any` |
4085+
| `isLoading` | `boolean` |
4086+
| `isValidating` | `boolean` |
4087+
| `mutate` | `KeyedMutator`<[`FetchResponse`](interfaces/FetchResponse.md)<{ `results`: [`AttachmentResponse`](interfaces/AttachmentResponse.md)[] }\>\> |
4088+
4089+
#### Defined in
4090+
4091+
[packages/framework/esm-react-utils/src/useAttachments.ts:5](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-react-utils/src/useAttachments.ts#L5)
4092+
4093+
___
4094+
39664095
## Store Functions
39674096

39684097
### createGlobalStore
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
[@openmrs/esm-framework](../API.md) / Attachment
2+
3+
# Interface: Attachment
4+
5+
## Table of contents
6+
7+
### Properties
8+
9+
- [bytesContentFamily](Attachment.md#bytescontentfamily)
10+
- [bytesMimeType](Attachment.md#bytesmimetype)
11+
- [dateTime](Attachment.md#datetime)
12+
- [description](Attachment.md#description)
13+
- [id](Attachment.md#id)
14+
- [src](Attachment.md#src)
15+
- [title](Attachment.md#title)
16+
17+
## Properties
18+
19+
### bytesContentFamily
20+
21+
**bytesContentFamily**: `string`
22+
23+
#### Defined in
24+
25+
[packages/framework/esm-api/src/types/attachments-types.ts:17](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L17)
26+
27+
___
28+
29+
### bytesMimeType
30+
31+
**bytesMimeType**: `string`
32+
33+
#### Defined in
34+
35+
[packages/framework/esm-api/src/types/attachments-types.ts:16](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L16)
36+
37+
___
38+
39+
### dateTime
40+
41+
**dateTime**: `string`
42+
43+
#### Defined in
44+
45+
[packages/framework/esm-api/src/types/attachments-types.ts:15](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L15)
46+
47+
___
48+
49+
### description
50+
51+
**description**: `string`
52+
53+
#### Defined in
54+
55+
[packages/framework/esm-api/src/types/attachments-types.ts:14](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L14)
56+
57+
___
58+
59+
### id
60+
61+
**id**: `string`
62+
63+
#### Defined in
64+
65+
[packages/framework/esm-api/src/types/attachments-types.ts:11](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L11)
66+
67+
___
68+
69+
### src
70+
71+
**src**: `string`
72+
73+
#### Defined in
74+
75+
[packages/framework/esm-api/src/types/attachments-types.ts:12](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L12)
76+
77+
___
78+
79+
### title
80+
81+
**title**: `string`
82+
83+
#### Defined in
84+
85+
[packages/framework/esm-api/src/types/attachments-types.ts:13](https://github.com/openmrs/openmrs-esm-core/blob/main/packages/framework/esm-api/src/types/attachments-types.ts#L13)

0 commit comments

Comments
 (0)