@hackmd/api 2.2.1 → 2.4.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/dist/error.d.ts CHANGED
@@ -10,4 +10,12 @@ declare class MissingRequiredArgument extends HackMDError {
10
10
  }
11
11
  declare class InternalServerError extends HttpResponseError {
12
12
  }
13
- export { HackMDError, HttpResponseError, MissingRequiredArgument, InternalServerError };
13
+ declare class TooManyRequestsError extends HttpResponseError {
14
+ readonly code: number;
15
+ readonly statusText: string;
16
+ readonly userLimit: number;
17
+ readonly userRemaining: number;
18
+ readonly resetAfter?: number | undefined;
19
+ constructor(message: string, code: number, statusText: string, userLimit: number, userRemaining: number, resetAfter?: number | undefined);
20
+ }
21
+ export { HackMDError, HttpResponseError, MissingRequiredArgument, InternalServerError, TooManyRequestsError, };
package/dist/error.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TooManyRequestsError = exports.InternalServerError = exports.MissingRequiredArgument = exports.HttpResponseError = exports.HackMDError = void 0;
3
4
  class HackMDError extends Error {
4
5
  constructor(message) {
5
6
  super(message);
@@ -21,3 +22,14 @@ exports.MissingRequiredArgument = MissingRequiredArgument;
21
22
  class InternalServerError extends HttpResponseError {
22
23
  }
23
24
  exports.InternalServerError = InternalServerError;
25
+ class TooManyRequestsError extends HttpResponseError {
26
+ constructor(message, code, statusText, userLimit, userRemaining, resetAfter) {
27
+ super(message, code, statusText);
28
+ this.code = code;
29
+ this.statusText = statusText;
30
+ this.userLimit = userLimit;
31
+ this.userRemaining = userRemaining;
32
+ this.resetAfter = resetAfter;
33
+ }
34
+ }
35
+ exports.TooManyRequestsError = TooManyRequestsError;
package/dist/index.d.ts CHANGED
@@ -1,22 +1,36 @@
1
1
  import { AxiosResponse } from 'axios';
2
- import { CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, DeleteUserNote, DeleteTeamNote, UpdateUserNote, SingleNote, UpdateTeamNote } from './type';
3
- export default class API {
2
+ import { CreateNoteOptions, GetMe, GetUserHistory, GetUserNotes, GetUserNote, CreateUserNote, GetUserTeams, GetTeamNotes, CreateTeamNote, SingleNote } from './type';
3
+ export type RequestOptions = {
4
+ unwrapData?: boolean;
5
+ };
6
+ type OptionReturnType<Opt, T> = Opt extends {
7
+ unwrapData: false;
8
+ } ? AxiosResponse<T> : Opt extends {
9
+ unwrapData: true;
10
+ } ? T : T;
11
+ export type APIClientOptions = {
12
+ wrapResponseErrors: boolean;
13
+ };
14
+ export declare class API {
4
15
  readonly accessToken: string;
5
16
  hackmdAPIEndpointURL: string;
17
+ options: APIClientOptions;
6
18
  private axios;
7
- constructor(accessToken: string, hackmdAPIEndpointURL?: string);
8
- getMe(): Promise<GetMe>;
9
- getHistory(): Promise<GetUserHistory>;
10
- getNoteList(): Promise<GetUserNotes>;
11
- getNote(noteId: string): Promise<GetUserNote>;
12
- createNote(options: CreateNoteOptions): Promise<CreateUserNote>;
13
- updateNoteContent(noteId: string, content?: string): Promise<UpdateUserNote>;
14
- updateNote(noteId: string, options: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>): Promise<AxiosResponse>;
15
- deleteNote(noteId: string): Promise<DeleteUserNote>;
16
- getTeams(): Promise<GetUserTeams>;
17
- getTeamNotes(teamPath: string): Promise<GetTeamNotes>;
18
- createTeamNote(teamPath: string, options: CreateNoteOptions): Promise<CreateTeamNote>;
19
- updateTeamNoteContent(teamPath: string, noteId: string, content?: string): Promise<UpdateTeamNote>;
19
+ constructor(accessToken: string, hackmdAPIEndpointURL?: string, options?: APIClientOptions);
20
+ getMe<Opt extends RequestOptions>(options?: Opt): Promise<OptionReturnType<Opt, GetMe>>;
21
+ getHistory<Opt extends RequestOptions>(options?: Opt): Promise<OptionReturnType<Opt, GetUserHistory>>;
22
+ getNoteList<Opt extends RequestOptions>(options?: Opt): Promise<OptionReturnType<Opt, GetUserNotes>>;
23
+ getNote<Opt extends RequestOptions>(noteId: string, options?: Opt): Promise<OptionReturnType<Opt, GetUserNote>>;
24
+ createNote<Opt extends RequestOptions>(payload: CreateNoteOptions, options?: Opt): Promise<OptionReturnType<Opt, CreateUserNote>>;
25
+ updateNoteContent<Opt extends RequestOptions>(noteId: string, content?: string, options?: Opt): Promise<OptionReturnType<Opt, SingleNote>>;
26
+ updateNote<Opt extends RequestOptions>(noteId: string, payload: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>, options?: Opt): Promise<OptionReturnType<Opt, SingleNote>>;
27
+ deleteNote<Opt extends RequestOptions>(noteId: string, options?: Opt): Promise<OptionReturnType<Opt, SingleNote>>;
28
+ getTeams<Opt extends RequestOptions>(options?: Opt): Promise<OptionReturnType<Opt, GetUserTeams>>;
29
+ getTeamNotes<Opt extends RequestOptions>(teamPath: string, options?: Opt): Promise<OptionReturnType<Opt, GetTeamNotes>>;
30
+ createTeamNote<Opt extends RequestOptions>(teamPath: string, payload: CreateNoteOptions, options?: Opt): Promise<OptionReturnType<Opt, CreateTeamNote>>;
31
+ updateTeamNoteContent(teamPath: string, noteId: string, content?: string): Promise<AxiosResponse>;
20
32
  updateTeamNote(teamPath: string, noteId: string, options: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>): Promise<AxiosResponse>;
21
- deleteTeamNote(teamPath: string, noteId: string): Promise<DeleteTeamNote>;
33
+ deleteTeamNote(teamPath: string, noteId: string): Promise<AxiosResponse>;
34
+ private unwrapData;
22
35
  }
36
+ export default API;
package/dist/index.js CHANGED
@@ -1,12 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.API = void 0;
3
4
  const tslib_1 = require("tslib");
4
5
  const axios_1 = tslib_1.__importDefault(require("axios"));
5
6
  const HackMDErrors = tslib_1.__importStar(require("./error"));
7
+ const defaultOption = {
8
+ unwrapData: true,
9
+ };
6
10
  class API {
7
- constructor(accessToken, hackmdAPIEndpointURL = "https://api.hackmd.io/v1") {
11
+ constructor(accessToken, hackmdAPIEndpointURL = "https://api.hackmd.io/v1", options = { wrapResponseErrors: true }) {
8
12
  this.accessToken = accessToken;
9
13
  this.hackmdAPIEndpointURL = hackmdAPIEndpointURL;
14
+ this.options = options;
10
15
  if (!accessToken) {
11
16
  throw new HackMDErrors.MissingRequiredArgument('Missing access token when creating HackMD client');
12
17
  }
@@ -22,69 +27,75 @@ class API {
22
27
  }, (err) => {
23
28
  return Promise.reject(err);
24
29
  });
25
- this.axios.interceptors.response.use((response) => {
26
- return response;
27
- }, async (err) => {
28
- if (!err.response) {
29
- return Promise.reject(err);
30
- }
31
- if (err.response.status >= 500) {
32
- throw new HackMDErrors.InternalServerError(`HackMD internal error (${err.response.status} ${err.response.statusText})`, err.response.status, err.response.statusText);
33
- }
34
- else {
35
- throw new HackMDErrors.HttpResponseError(`Received an error response (${err.response.status} ${err.response.statusText}) from HackMD`, err.response.status, err.response.statusText);
36
- }
37
- });
30
+ if (options.wrapResponseErrors) {
31
+ this.axios.interceptors.response.use((response) => {
32
+ return response;
33
+ }, async (err) => {
34
+ if (!err.response) {
35
+ return Promise.reject(err);
36
+ }
37
+ if (err.response.status >= 500) {
38
+ throw new HackMDErrors.InternalServerError(`HackMD internal error (${err.response.status} ${err.response.statusText})`, err.response.status, err.response.statusText);
39
+ }
40
+ else if (err.response.status === 429) {
41
+ throw new HackMDErrors.TooManyRequestsError(`Too many requests (${err.response.status} ${err.response.statusText})`, err.response.status, err.response.statusText, parseInt(err.response.headers['x-ratelimit-userlimit'], 10), parseInt(err.response.headers['x-ratelimit-userremaining'], 10), parseInt(err.response.headers['x-ratelimit-userreset'], 10));
42
+ }
43
+ else {
44
+ throw new HackMDErrors.HttpResponseError(`Received an error response (${err.response.status} ${err.response.statusText}) from HackMD`, err.response.status, err.response.statusText);
45
+ }
46
+ });
47
+ }
38
48
  }
39
- async getMe() {
40
- const { data } = await this.axios.get("me");
41
- return data;
49
+ async getMe(options = defaultOption) {
50
+ return this.unwrapData(this.axios.get("me"), options.unwrapData);
42
51
  }
43
- async getHistory() {
44
- const { data } = await this.axios.get("history");
45
- return data;
52
+ async getHistory(options = defaultOption) {
53
+ return this.unwrapData(this.axios.get("history"), options.unwrapData);
46
54
  }
47
- async getNoteList() {
48
- const { data } = await this.axios.get("notes");
49
- return data;
55
+ async getNoteList(options = defaultOption) {
56
+ return this.unwrapData(this.axios.get("notes"), options.unwrapData);
50
57
  }
51
- async getNote(noteId) {
52
- const { data } = await this.axios.get(`notes/${noteId}`);
53
- return data;
58
+ async getNote(noteId, options = defaultOption) {
59
+ return this.unwrapData(this.axios.get(`notes/${noteId}`), options.unwrapData);
54
60
  }
55
- async createNote(options) {
56
- const { data } = await this.axios.post("notes", options);
57
- return data;
61
+ async createNote(payload, options = defaultOption) {
62
+ return this.unwrapData(this.axios.post("notes", payload), options.unwrapData);
58
63
  }
59
- async updateNoteContent(noteId, content) {
60
- await this.axios.patch(`notes/${noteId}`, { content });
64
+ async updateNoteContent(noteId, content, options = defaultOption) {
65
+ return this.unwrapData(this.axios.patch(`notes/${noteId}`, { content }), options.unwrapData);
61
66
  }
62
- async updateNote(noteId, options) {
63
- return await this.axios.patch(`notes/${noteId}`, options);
67
+ async updateNote(noteId, payload, options = defaultOption) {
68
+ return this.unwrapData(this.axios.patch(`notes/${noteId}`, payload), options.unwrapData);
64
69
  }
65
- async deleteNote(noteId) {
66
- await this.axios.delete(`notes/${noteId}`);
70
+ async deleteNote(noteId, options = defaultOption) {
71
+ return this.unwrapData(this.axios.delete(`notes/${noteId}`), options.unwrapData);
67
72
  }
68
- async getTeams() {
69
- const { data } = await this.axios.get("teams");
70
- return data;
73
+ async getTeams(options = defaultOption) {
74
+ return this.unwrapData(this.axios.get("teams"), options.unwrapData);
71
75
  }
72
- async getTeamNotes(teamPath) {
73
- const { data } = await this.axios.get(`teams/${teamPath}/notes`);
74
- return data;
76
+ async getTeamNotes(teamPath, options = defaultOption) {
77
+ return this.unwrapData(this.axios.get(`teams/${teamPath}/notes`), options.unwrapData);
75
78
  }
76
- async createTeamNote(teamPath, options) {
77
- const { data } = await this.axios.post(`teams/${teamPath}/notes`, options);
78
- return data;
79
+ async createTeamNote(teamPath, payload, options = defaultOption) {
80
+ return this.unwrapData(this.axios.post(`teams/${teamPath}/notes`, payload), options.unwrapData);
79
81
  }
80
82
  async updateTeamNoteContent(teamPath, noteId, content) {
81
- await this.axios.patch(`teams/${teamPath}/notes/${noteId}`, { content });
83
+ return this.axios.patch(`teams/${teamPath}/notes/${noteId}`, { content });
82
84
  }
83
85
  async updateTeamNote(teamPath, noteId, options) {
84
- return await this.axios.patch(`teams/${teamPath}/notes/${noteId}`, options);
86
+ return this.axios.patch(`teams/${teamPath}/notes/${noteId}`, options);
85
87
  }
86
88
  async deleteTeamNote(teamPath, noteId) {
87
- await this.axios.delete(`teams/${teamPath}/notes/${noteId}`);
89
+ return this.axios.delete(`teams/${teamPath}/notes/${noteId}`);
90
+ }
91
+ unwrapData(reqP, unwrap = true) {
92
+ if (unwrap) {
93
+ return reqP.then(response => response.data);
94
+ }
95
+ else {
96
+ return reqP;
97
+ }
88
98
  }
89
99
  }
100
+ exports.API = API;
90
101
  exports.default = API;
package/dist/type.d.ts CHANGED
@@ -15,7 +15,7 @@ export declare enum CommentPermissionType {
15
15
  SIGNED_IN_USERS = "signed_in_users",
16
16
  EVERYONE = "everyone"
17
17
  }
18
- export declare type CreateNoteOptions = {
18
+ export type CreateNoteOptions = {
19
19
  title?: string;
20
20
  content?: string;
21
21
  readPermission?: NotePermissionRole;
@@ -23,7 +23,7 @@ export declare type CreateNoteOptions = {
23
23
  commentPermission?: CommentPermissionType;
24
24
  permalink?: string;
25
25
  };
26
- export declare type Team = {
26
+ export type Team = {
27
27
  id: string;
28
28
  ownerId: string;
29
29
  name: string;
@@ -34,7 +34,7 @@ export declare type Team = {
34
34
  visibility: TeamVisibilityType;
35
35
  createdAt: Date;
36
36
  };
37
- export declare type User = {
37
+ export type User = {
38
38
  id: string;
39
39
  email: string | null;
40
40
  name: string;
@@ -42,7 +42,7 @@ export declare type User = {
42
42
  photo: string;
43
43
  teams: Team[];
44
44
  };
45
- export declare type SimpleUserProfile = {
45
+ export type SimpleUserProfile = {
46
46
  name: string;
47
47
  userPath: string;
48
48
  photo: string;
@@ -54,7 +54,7 @@ export declare enum NotePermissionRole {
54
54
  SIGNED_IN = "signed_in",
55
55
  GUEST = "guest"
56
56
  }
57
- export declare type Note = {
57
+ export type Note = {
58
58
  id: string;
59
59
  title: string;
60
60
  tags: string[];
@@ -71,18 +71,18 @@ export declare type Note = {
71
71
  readPermission: NotePermissionRole;
72
72
  writePermission: NotePermissionRole;
73
73
  };
74
- export declare type SingleNote = Note & {
74
+ export type SingleNote = Note & {
75
75
  content: string;
76
76
  };
77
- export declare type GetMe = User;
78
- export declare type GetUserNotes = Note[];
79
- export declare type GetUserNote = SingleNote;
80
- export declare type GetUserHistory = Note[];
81
- export declare type CreateUserNote = SingleNote;
82
- export declare type UpdateUserNote = void;
83
- export declare type DeleteUserNote = void;
84
- export declare type GetUserTeams = Team[];
85
- export declare type GetTeamNotes = Note[];
86
- export declare type CreateTeamNote = SingleNote;
87
- export declare type UpdateTeamNote = void;
88
- export declare type DeleteTeamNote = void;
77
+ export type GetMe = User;
78
+ export type GetUserNotes = Note[];
79
+ export type GetUserNote = SingleNote;
80
+ export type GetUserHistory = Note[];
81
+ export type CreateUserNote = SingleNote;
82
+ export type UpdateUserNote = void;
83
+ export type DeleteUserNote = void;
84
+ export type GetUserTeams = Team[];
85
+ export type GetTeamNotes = Note[];
86
+ export type CreateTeamNote = SingleNote;
87
+ export type UpdateTeamNote = void;
88
+ export type DeleteTeamNote = void;
package/dist/type.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotePermissionRole = exports.CommentPermissionType = exports.NotePublishType = exports.TeamVisibilityType = void 0;
3
4
  var TeamVisibilityType;
4
5
  (function (TeamVisibilityType) {
5
6
  TeamVisibilityType["PUBLIC"] = "public";
package/package.json CHANGED
@@ -1,14 +1,16 @@
1
1
  {
2
2
  "name": "@hackmd/api",
3
- "version": "2.2.1",
3
+ "version": "2.4.0",
4
4
  "description": "HackMD Node.js API Client",
5
5
  "main": "dist/index.js",
6
6
  "declaration": "./dist/index.d.ts",
7
7
  "scripts": {
8
- "build": "tsc -d",
9
- "watch": "tsc -w",
8
+ "clean": "rimraf dist",
9
+ "build": "npm run clean && tsc -p tsconfig.build.json -d",
10
+ "watch": "npm run clean && tsc -p tsconfig.build.json -w",
10
11
  "prepublishOnly": "npm run build",
11
- "lint": "eslint src --fix --ext .ts"
12
+ "lint": "eslint src --fix --ext .ts",
13
+ "test": "jest"
12
14
  },
13
15
  "keywords": [
14
16
  "HackMD",
@@ -26,13 +28,20 @@
26
28
  },
27
29
  "license": "MIT",
28
30
  "devDependencies": {
31
+ "@faker-js/faker": "^7.6.0",
32
+ "@types/eslint": "^8.21.0",
33
+ "@types/jest": "^29.4.0",
29
34
  "@types/node": "^13.11.1",
30
- "@typescript-eslint/eslint-plugin": "^5.12.0",
35
+ "@typescript-eslint/eslint-plugin": "^5.52.0",
36
+ "@typescript-eslint/parser": "^5.52.0",
37
+ "dotenv": "^16.0.3",
31
38
  "eslint": "^8.9.0",
32
- "eslint-plugin-typescript": "^0.14.0",
39
+ "jest": "^29.4.2",
40
+ "msw": "^1.0.1",
41
+ "rimraf": "^4.1.2",
42
+ "ts-jest": "^29.0.5",
33
43
  "ts-node": "^8.8.2",
34
- "typescript": "^3.8.3",
35
- "typescript-eslint-parser": "^22.0.0"
44
+ "typescript": "^4.9.5"
36
45
  },
37
46
  "dependencies": {
38
47
  "axios": "^0.25.0",
package/dist/utils.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function defaults(defaultObject: any, ...mergeObjects: any[]): any;
package/dist/utils.js DELETED
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- function defaults(defaultObject, ...mergeObjects) {
4
- return mergeObjects.reduce((acc, cur) => {
5
- return Object.entries(cur).reduce((accc, [key, value]) => {
6
- if (typeof accc[key] === 'undefined') {
7
- return Object.assign(Object.assign({}, accc), { [key]: value });
8
- }
9
- else {
10
- return accc;
11
- }
12
- }, Object.assign({}, acc));
13
- }, Object.assign({}, defaultObject));
14
- }
15
- exports.defaults = defaults;