Skip to content

Commit cd5f294

Browse files
authored
Merge pull request #11 from Mermaid-Chart/feat/add-setDocument-to-sdk
feat: add `setDocument(document)` API function
2 parents 37438d0 + 3a4066d commit cd5f294

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

openapi.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ components:
7878
format: uuid
7979
description: |
8080
The id of this diagram, required for `setDocument()`.
81+
code:
82+
type: string
83+
description: |
84+
The Mermaid
85+
[`application/vnd.mermaid`](https://www.iana.org/assignments/media-types/application/vnd.mermaid)
86+
code for this diagram.
8187
required:
8288
- documentID
8389
- id
@@ -246,6 +252,42 @@ paths:
246252
$ref: '#/components/schemas/MCDocument'
247253
'404':
248254
description: File Not Found
255+
put:
256+
tags:
257+
- document
258+
summary: Update the given document.
259+
operationId: setDocument
260+
parameters:
261+
- name: documentID
262+
in: path
263+
required: true
264+
description: The ID of the document to update.
265+
schema:
266+
type: string
267+
requestBody:
268+
description: Document and diagram settings to update.
269+
required: true
270+
content:
271+
application/json:
272+
schema:
273+
$ref: '#/components/schemas/MCDocument'
274+
security:
275+
- mermaidchart_auth: []
276+
responses:
277+
'200':
278+
description: |
279+
Update status.
280+
content:
281+
application/json:
282+
schema:
283+
type: object
284+
# TODO: update this once MC-1060 is fixed.
285+
# properties:
286+
# result:
287+
# type: string
288+
# enum:
289+
# - ok
290+
# - failed
249291
delete:
250292
tags:
251293
- document

packages/sdk/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Compile an ESM version of this codebase for Node.JS v18.
1111
- Add `MermaidChart#getDiagram(diagramID)` function to get a diagram.
1212
- Add `MermaidChart#createDocument(projectID)` function to create a digram in a project.
13+
- Add `MermaidChart#setDocument(document)` function to update a diagram.
1314
- Add `MermaidChart#deleteDocument(documentID)` function to delete a diagram.
1415

1516
### Fixed
1617

1718
- Fix `MCDocument` `major`/`minor` type to `number`.
19+
- Add `code` field to `MCDocument` type
1820

1921
### Fixed
2022

packages/sdk/src/index.e2e.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,43 @@ describe('createDocument', () => {
7474
});
7575
});
7676

77+
describe('setDocument', () => {
78+
it('should set document', async() => {
79+
const newDocument = await client.createDocument(testProjectId);
80+
documentsToDelete.add(newDocument.documentID);
81+
82+
const code = `flowchart LR
83+
A(Generated by<br><code>@mermaidchart/sdk</code><br>E2E tests)`;
84+
85+
await client.setDocument({
86+
documentID: newDocument.documentID,
87+
id: newDocument.id, // diagram ID
88+
title: "@mermaidchart/sdk E2E test diagram",
89+
code,
90+
});
91+
92+
const updatedDoc = await client.getDocument({
93+
documentID: newDocument.documentID,
94+
});
95+
expect(updatedDoc).toMatchObject({
96+
title: "@mermaidchart/sdk E2E test diagram",
97+
code,
98+
});
99+
});
100+
101+
// TODO: this function never seems to return an error, see MC-1060
102+
it.skip('should throw an error on invalid data', async() => {
103+
const newDocument = await client.createDocument(testProjectId);
104+
documentsToDelete.add(newDocument.documentID);
105+
106+
await expect(client.setDocument({
107+
documentID: newDocument.documentID,
108+
// @ts-expect-error not setting diagram `id` should throw an error
109+
id: null,
110+
})).rejects.toThrowError("400"); // should throw HTTP 400 error
111+
});
112+
});
113+
77114
describe('deleteDocument', () => {
78115
it('should delete document', async() => {
79116
const newDocument = await client.createDocument(testProjectId);

packages/sdk/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ export class MermaidChart {
204204
return data;
205205
}
206206

207+
/**
208+
* Update the given document.
209+
*
210+
* @param document The document to update.
211+
*/
212+
public async setDocument(
213+
document: Pick<MCDocument, 'id' | 'documentID'> & Partial<MCDocument>,
214+
) {
215+
const {data} = await this.axios.put<{result: "ok"} | {result: "failed", error: any}>(
216+
URLS.rest.documents.pick(document).self,
217+
document,
218+
);
219+
220+
if (data.result === "failed") {
221+
throw new Error(
222+
`setDocument(${JSON.stringify({documentID: document.documentID})} failed due to ${JSON.stringify(data.error)}`
223+
);
224+
}
225+
}
226+
207227
/**
208228
* Delete the given document.
209229
* @param documentID The ID of the document to delete.

packages/sdk/src/types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ export interface DiagramDocument {
5151
documentID: string;
5252
major: number;
5353
minor: number;
54+
/**
55+
* The Mermaid
56+
* [`application/vnd.mermaid`](https://www.iana.org/assignments/media-types/application/vnd.mermaid)
57+
* code for this diagram.
58+
*/
59+
code?: string;
5460
}
5561

5662
export interface AuthorizationData {

0 commit comments

Comments
 (0)