Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the getDiagram(diagramID) API function to the SDK #8

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ components:
projectID:
type: string
major:
type: string
type: integer
minor:
type: string
type: integer
title:
type: string
required:
Expand Down Expand Up @@ -160,6 +160,43 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/Document'

/rest-api/documents/{documentID}:
get:
tags:
- document
summary: Gets the given diagram
operationId: getDocument
parameters:
- name: documentID
in: path
required: true
description: The ID of the document to get diagram data for
schema:
type: string
- name: version
in: query
required: false
description: |
The version of the document to get diagram data for.

If not set, defaults to the highest version.
schema:
type: string
example: v0.1
security:
- mermaidchart_auth:
- read
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/Document'
'404':
description: File Not Found

/raw/{documentID}:
get:
tags:
Expand Down
5 changes: 5 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Compile an ESM version of this codebase for Node.JS v18.
- Add `MermaidChart#getDiagram(diagramID)` function to get a diagram.

### Fixed

- Fix `MCDocument` `major`/`minor` type to `number`.

## [0.1.1] - 2023-09-08

Expand Down
41 changes: 41 additions & 0 deletions packages/sdk/src/index.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MermaidChart } from './index.js';
import { beforeAll, describe, expect, it } from 'vitest';

import process from 'node:process';
import { AxiosError } from 'axios';

let client: MermaidChart;

Expand Down Expand Up @@ -32,3 +33,43 @@ describe('getUser', () => {
expect(user).toHaveProperty('emailAddress');
});
});

const documentMatcher = expect.objectContaining({
documentID: expect.any(String),
major: expect.any(Number),
minor: expect.any(Number),
});

describe("getDocument", () => {
it("should get publicly shared diagram", async() => {
const latestDocument = await client.getDocument({
// owned by [email protected]
documentID: '8bce727b-69b7-4f6e-a434-d578e2b363ff',
});

expect(latestDocument).toStrictEqual(documentMatcher);

const earliestDocument = await client.getDocument({
// owned by [email protected]
documentID: '8bce727b-69b7-4f6e-a434-d578e2b363ff',
major: 0,
minor: 1,
});

expect(earliestDocument).toStrictEqual(documentMatcher);
});

it("should throw 404 on unknown document", async() => {
let error: AxiosError | undefined = undefined;
try {
await client.getDocument({
documentID: '00000000-0000-0000-0000-0000deaddead',
});
} catch (err) {
error = err as AxiosError;
}

expect(error).toBeInstanceOf(AxiosError);
expect(error?.response?.status).toBe(404);
});
});
7 changes: 7 additions & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,13 @@ export class MermaidChart {
return url;
}

public async getDocument(
document: Pick<MCDocument, 'documentID'> | Pick<MCDocument, 'documentID' | 'major' | 'minor'>,
) {
const {data} = await this.axios.get<MCDocument>(URLS.rest.documents.pick(document).self);
return data;
}

public async getRawDocument(
document: Pick<MCDocument, 'documentID' | 'major' | 'minor'>,
theme: 'light' | 'dark',
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ export interface MCProject {
export interface MCDocument {
documentID: string;
projectID: string;
major: string;
minor: string;
major: number;
minor: number;
title: string;
}

Expand Down
19 changes: 19 additions & 0 deletions packages/sdk/src/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@ export const URLS = {
token: `/oauth/token`,
},
rest: {
documents: {
pick: (opts: Pick<MCDocument, 'documentID'> | Pick<MCDocument, 'documentID' | 'major' | 'minor'>) => {
const {documentID} = opts;
let queryParams = "";

if ('major' in opts) {
const {major, minor} = opts;

queryParams = `v${major ?? 0}.${minor ?? 1}`;
}

const baseURL = `/rest-api/documents/${documentID}`;
return {
presentations: `${baseURL}/presentations`,
self: baseURL,
withVersion: `${baseURL}${queryParams}`
};
}
},
users: {
self: `/rest-api/users/me`,
},
Expand Down