Skip to content

Commit cee4d5b

Browse files
committed
2 parents 97e071e + 3fd5a09 commit cee4d5b

File tree

9 files changed

+102
-53
lines changed

9 files changed

+102
-53
lines changed

.eslintrc.cjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ module.exports = {
4848
'@typescript-eslint/consistent-type-imports': 'error',
4949
'@typescript-eslint/no-floating-promises': 'error',
5050
'@typescript-eslint/no-misused-promises': 'error',
51+
'@typescript-eslint/no-unused-vars': [
52+
'error',
53+
{
54+
// TypeScript by default allows params starting with _
55+
varsIgnorePattern: /^_/.source,
56+
argsIgnorePattern: /^_/.source,
57+
caughtErrorsIgnorePattern: /^_/.source,
58+
destructuredArrayIgnorePattern: /^_/.source,
59+
},
60+
],
5161
'@typescript-eslint/ban-ts-comment': [
5262
'error',
5363
{

cSpell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"version": "0.2",
33
"language": "en",
44
"words": [
5+
"Alois",
56
"Cataa",
67
"colour",
78
"Cookiebot",

packages/sdk/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Changes
9+
10+
- `MermaidChart#resetAccessToken()` no longer returns a `Promise`.
11+
812
## [0.2.0] - 2024-04-11
913

1014
### Added

packages/sdk/package.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
"build:code:browser": "esbuild src/index.ts --bundle --minify --outfile=dist/bundle.iife.js",
1515
"build:code:node": "esbuild src/index.ts --bundle --platform=node --target=node18.18 --format=esm --packages=external --minify --outfile=dist/index.mjs",
1616
"build:types": "tsc -p ./tsconfig.json --emitDeclarationOnly",
17+
"lint": "eslint src/ && prettier --check src/",
18+
"lint:fix": "eslint --fix src/ && prettier --write src/",
1719
"prepare": "pnpm run build",
1820
"test": "vitest src/",
1921
"test:e2e": "vitest --config vitest.config.e2e.ts"
@@ -31,7 +33,11 @@
3133
},
3234
"devDependencies": {
3335
"@types/node": "^18.18.0",
34-
"@types/uuid": "^9.0.2"
36+
"@types/uuid": "^9.0.2",
37+
"@typescript-eslint/eslint-plugin": "^6.11.0",
38+
"@typescript-eslint/parser": "^6.11.0",
39+
"eslint": "^8.54.0",
40+
"prettier": "^3.0.3"
3541
},
3642
"publishConfig": {
3743
"access": "public"

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

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,32 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
66

77
import process from 'node:process';
88
import { AxiosError } from 'axios';
9-
import { MCDocument } from './types.js';
9+
import type { MCDocument } from './types.js';
1010

1111
let testProjectId = '316557b3-cb6f-47ed-acf7-fcfb7ce188d5';
1212
let baseURL = new URL('https://test.mermaidchart.com');
1313

1414
let client: MermaidChart;
1515

16-
beforeAll(async() => {
16+
beforeAll(async () => {
1717
if (process.env.TEST_MERMAIDCHART_BASE_URL) {
1818
try {
1919
baseURL = new URL(process.env.TEST_MERMAIDCHART_BASE_URL);
2020
} catch (err) {
21-
throw new Error("Invalid URL in environment variable TEST_MERMAIDCHART_BASE_URL", { cause: err});
21+
throw new Error('Invalid URL in environment variable TEST_MERMAIDCHART_BASE_URL', {
22+
cause: err,
23+
});
2224
}
2325
} else {
24-
process.emitWarning(`Missing environment variable TEST_MERMAIDCHART_BASE_URL. Defaulting to ${baseURL.href}.`);
26+
process.emitWarning(
27+
`Missing environment variable TEST_MERMAIDCHART_BASE_URL. Defaulting to ${baseURL.href}.`,
28+
);
2529
}
2630

2731
if (!process.env.TEST_MERMAIDCHART_API_TOKEN) {
2832
throw new Error(
29-
"Missing required environment variable TEST_MERMAIDCHART_API_TOKEN. "
30-
+ `Please go to ${new URL('/app/user/settings', baseURL)} and create one.`
33+
'Missing required environment variable TEST_MERMAIDCHART_API_TOKEN. ' +
34+
`Please go to ${new URL('/app/user/settings', baseURL)} and create one.`,
3135
);
3236
}
3337

@@ -44,25 +48,30 @@ beforeAll(async() => {
4448
// confirm that testProjectId is valid
4549
if (process.env.TEST_MERMAIDCHART_PROJECT_ID) {
4650
testProjectId = process.env.TEST_MERMAIDCHART_PROJECT_ID;
47-
if (!projects.find((project) => project.id === testProjectId)) {
51+
if (!projects.some((project) => project.id === testProjectId)) {
4852
throw new Error(
49-
`Invalid environment variable TEST_MERMAIDCHART_PROJECT_ID. `
50-
+ `Please go to ${new URL('/app/projects', baseURL)} and create one that you have access to.`
53+
`Invalid environment variable TEST_MERMAIDCHART_PROJECT_ID. ` +
54+
`Please go to ${new URL(
55+
'/app/projects',
56+
baseURL,
57+
)} and create one that you have access to.`,
5158
);
5259
}
5360
} else {
54-
if (!projects.find((project) => project.id === testProjectId)) {
61+
if (!projects.some((project) => project.id === testProjectId)) {
5562
throw new Error(
56-
`Missing environment variable TEST_MERMAIDCHART_PROJECT_ID. `
57-
+ `Please go to ${new URL('/app/projects', baseURL)} and create one.`
63+
`Missing environment variable TEST_MERMAIDCHART_PROJECT_ID. ` +
64+
`Please go to ${new URL('/app/projects', baseURL)} and create one.`,
5865
);
5966
}
60-
process.emitWarning(`Missing optional environment variable TEST_MERMAIDCHART_PROJECT_ID. Defaulting to ${testProjectId}`);
67+
process.emitWarning(
68+
`Missing optional environment variable TEST_MERMAIDCHART_PROJECT_ID. Defaulting to ${testProjectId}`,
69+
);
6170
}
6271
});
6372

6473
describe('getUser', () => {
65-
it("should get user", async() => {
74+
it('should get user', async () => {
6675
const user = await client.getUser();
6776

6877
expect(user).toHaveProperty('emailAddress');
@@ -78,17 +87,19 @@ const documentMatcher = expect.objectContaining({
7887
/**
7988
* Cleanup created documents at the end of this test.
8089
*/
81-
const documentsToDelete = new Set<MCDocument["documentID"]>();
82-
afterAll(async() => {
83-
await Promise.all(Array.from(documentsToDelete).map(async(document) => {
84-
if (documentsToDelete.delete(document)) {
85-
await client.deleteDocument(document);
86-
}
87-
}));
90+
const documentsToDelete = new Set<MCDocument['documentID']>();
91+
afterAll(async () => {
92+
await Promise.all(
93+
[...documentsToDelete].map(async (document) => {
94+
if (documentsToDelete.delete(document)) {
95+
await client.deleteDocument(document);
96+
}
97+
}),
98+
);
8899
});
89100

90101
describe('createDocument', () => {
91-
it('should create document in project', async() => {
102+
it('should create document in project', async () => {
92103
const existingDocuments = await client.getDocuments(testProjectId);
93104

94105
const newDocument = await client.createDocument(testProjectId);
@@ -105,7 +116,7 @@ describe('createDocument', () => {
105116
});
106117

107118
describe('setDocument', () => {
108-
it('should set document', async() => {
119+
it('should set document', async () => {
109120
const newDocument = await client.createDocument(testProjectId);
110121
documentsToDelete.add(newDocument.documentID);
111122

@@ -115,33 +126,35 @@ describe('setDocument', () => {
115126
await client.setDocument({
116127
documentID: newDocument.documentID,
117128
projectID: newDocument.projectID,
118-
title: "@mermaidchart/sdk E2E test diagram",
129+
title: '@mermaidchart/sdk E2E test diagram',
119130
code,
120131
});
121132

122133
const updatedDoc = await client.getDocument({
123134
documentID: newDocument.documentID,
124135
});
125136
expect(updatedDoc).toMatchObject({
126-
title: "@mermaidchart/sdk E2E test diagram",
137+
title: '@mermaidchart/sdk E2E test diagram',
127138
code,
128139
});
129140
});
130141

131-
it('should throw an error on invalid data', async() => {
142+
it('should throw an error on invalid data', async () => {
132143
const newDocument = await client.createDocument(testProjectId);
133144
documentsToDelete.add(newDocument.documentID);
134145

135-
await expect(client.setDocument({
136-
documentID: newDocument.documentID,
137-
// @ts-expect-error not setting diagram `projectID` should throw an error
138-
projectID: null,
139-
})).rejects.toThrowError("400"); // should throw HTTP 400 error
146+
await expect(
147+
client.setDocument({
148+
documentID: newDocument.documentID,
149+
// @ts-expect-error not setting diagram `projectID` should throw an error
150+
projectID: null,
151+
}),
152+
).rejects.toThrowError('400'); // should throw HTTP 400 error
140153
});
141154
});
142155

143156
describe('deleteDocument', () => {
144-
it('should delete document', async() => {
157+
it('should delete document', async () => {
145158
const newDocument = await client.createDocument(testProjectId);
146159

147160
expect(await client.getDocuments(testProjectId)).toContainEqual(newDocument);
@@ -154,8 +167,8 @@ describe('deleteDocument', () => {
154167
});
155168
});
156169

157-
describe("getDocument", () => {
158-
it("should get diagram", async() => {
170+
describe('getDocument', () => {
171+
it('should get diagram', async () => {
159172
const newDocument = await client.createDocument(testProjectId);
160173

161174
documentsToDelete.add(newDocument.documentID);
@@ -176,7 +189,7 @@ describe("getDocument", () => {
176189
expect(earliestDocument).toStrictEqual(documentMatcher);
177190
});
178191

179-
it("should throw 404 on unknown document", async() => {
192+
it('should throw 404 on unknown document', async () => {
180193
let error: AxiosError | undefined = undefined;
181194
try {
182195
await client.getDocument({
@@ -190,4 +203,3 @@ describe("getDocument", () => {
190203
expect(error?.response?.status).toBe(404);
191204
});
192205
});
193-

packages/sdk/src/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest';
22
import { MermaidChart } from './index.js';
3-
import { AuthorizationData } from './types.js';
3+
import type { AuthorizationData } from './types.js';
44

55
import { OAuth2Client } from '@badgateway/oauth2-client';
66

packages/sdk/src/index.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class MermaidChart {
143143
/**
144144
* This method is used after authentication to save the access token.
145145
* It should be called by the plugins if any update to access token is made outside this lib.
146-
* @param accessToken access token to use for requests
146+
* @param accessToken - access token to use for requests
147147
*/
148148
public async setAccessToken(accessToken: string): Promise<void> {
149149
this.axios.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
@@ -152,7 +152,7 @@ export class MermaidChart {
152152
this.accessToken = accessToken;
153153
}
154154

155-
public async resetAccessToken(): Promise<void> {
155+
public resetAccessToken(): void {
156156
this.accessToken = undefined;
157157
this.axios.defaults.headers.common['Authorization'] = `Bearer none`;
158158
}
@@ -204,38 +204,40 @@ export class MermaidChart {
204204
public async getDocument(
205205
document: Pick<MCDocument, 'documentID'> | Pick<MCDocument, 'documentID' | 'major' | 'minor'>,
206206
) {
207-
const {data} = await this.axios.get<MCDocument>(URLS.rest.documents.pick(document).self);
207+
const { data } = await this.axios.get<MCDocument>(URLS.rest.documents.pick(document).self);
208208
return data;
209209
}
210210

211211
/**
212212
* Update the given document.
213213
*
214-
* @param document The document to update.
214+
* @param document - The document to update.
215215
*/
216216
public async setDocument(
217217
document: Pick<MCDocument, 'documentID' | 'projectID'> & Partial<MCDocument>,
218218
) {
219-
const {data} = await this.axios.put<{result: "ok"} | {result: "failed", error: any}>(
219+
const { data } = await this.axios.put<{ result: 'ok' } | { result: 'failed'; error: unknown }>(
220220
URLS.rest.documents.pick(document).self,
221221
document,
222222
);
223223

224-
if (data.result === "failed") {
224+
if (data.result === 'failed') {
225225
throw new Error(
226-
`setDocument(${JSON.stringify({documentID: document.documentID})} failed due to ${JSON.stringify(data.error)}`
226+
`setDocument(${JSON.stringify({
227+
documentID: document.documentID,
228+
})} failed due to ${JSON.stringify(data.error)}`,
227229
);
228230
}
229231
}
230232

231233
/**
232234
* Delete the given document.
233-
* @param documentID The ID of the document to delete.
235+
* @param documentID - The ID of the document to delete.
234236
* @returns Metadata about the deleted document.
235237
*/
236238
public async deleteDocument(documentID: MCDocument['documentID']) {
237239
const deletedDocument = await this.axios.delete<Document>(
238-
URLS.rest.documents.pick({documentID}).self,
240+
URLS.rest.documents.pick({ documentID }).self,
239241
{}, // force sending empty JSON to avoid triggering CSRF check
240242
);
241243
return deletedDocument.data;

packages/sdk/src/urls.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ export const URLS = {
77
},
88
rest: {
99
documents: {
10-
pick: (opts: Pick<MCDocument, 'documentID'> | Pick<MCDocument, 'documentID' | 'major' | 'minor'>) => {
11-
const {documentID} = opts;
12-
let queryParams = "";
10+
pick: (
11+
opts: Pick<MCDocument, 'documentID'> | Pick<MCDocument, 'documentID' | 'major' | 'minor'>,
12+
) => {
13+
const { documentID } = opts;
14+
let queryParams = '';
1315

1416
if ('major' in opts) {
15-
const {major, minor} = opts;
17+
const { major, minor } = opts;
1618

1719
queryParams = `v${major ?? 0}.${minor ?? 1}`;
1820
}
@@ -21,9 +23,9 @@ export const URLS = {
2123
return {
2224
presentations: `${baseURL}/presentations`,
2325
self: baseURL,
24-
withVersion: `${baseURL}${queryParams}`
26+
withVersion: `${baseURL}${queryParams}`,
2527
};
26-
}
28+
},
2729
},
2830
users: {
2931
self: `/rest-api/users/me`,

pnpm-lock.yaml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)