@hackmd/api 2.2.1 → 2.3.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.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InternalServerError = exports.MissingRequiredArgument = exports.HttpResponseError = exports.HackMDError = void 0;
3
4
  class HackMDError extends Error {
4
5
  constructor(message) {
5
6
  super(message);
package/dist/index.d.ts CHANGED
@@ -1,22 +1,32 @@
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 declare class API {
4
12
  readonly accessToken: string;
5
13
  hackmdAPIEndpointURL: string;
6
14
  private axios;
7
15
  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>;
16
+ getMe<Opt extends RequestOptions>(options?: Opt): Promise<OptionReturnType<Opt, GetMe>>;
17
+ getHistory<Opt extends RequestOptions>(options?: Opt): Promise<OptionReturnType<Opt, GetUserHistory>>;
18
+ getNoteList<Opt extends RequestOptions>(options?: Opt): Promise<OptionReturnType<Opt, GetUserNotes>>;
19
+ getNote<Opt extends RequestOptions>(noteId: string, options?: Opt): Promise<OptionReturnType<Opt, GetUserNote>>;
20
+ createNote<Opt extends RequestOptions>(payload: CreateNoteOptions, options?: Opt): Promise<OptionReturnType<Opt, CreateUserNote>>;
21
+ updateNoteContent<Opt extends RequestOptions>(noteId: string, content?: string, options?: Opt): Promise<OptionReturnType<Opt, SingleNote>>;
22
+ updateNote<Opt extends RequestOptions>(noteId: string, payload: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>, options?: Opt): Promise<OptionReturnType<Opt, SingleNote>>;
23
+ deleteNote<Opt extends RequestOptions>(noteId: string, options?: Opt): Promise<OptionReturnType<Opt, SingleNote>>;
24
+ getTeams<Opt extends RequestOptions>(options?: Opt): Promise<OptionReturnType<Opt, GetUserTeams>>;
25
+ getTeamNotes<Opt extends RequestOptions>(teamPath: string, options?: Opt): Promise<OptionReturnType<Opt, GetTeamNotes>>;
26
+ createTeamNote<Opt extends RequestOptions>(teamPath: string, payload: CreateNoteOptions, options?: Opt): Promise<OptionReturnType<Opt, CreateTeamNote>>;
27
+ updateTeamNoteContent(teamPath: string, noteId: string, content?: string): Promise<AxiosResponse>;
20
28
  updateTeamNote(teamPath: string, noteId: string, options: Partial<Pick<SingleNote, 'content' | 'readPermission' | 'writePermission' | 'permalink'>>): Promise<AxiosResponse>;
21
- deleteTeamNote(teamPath: string, noteId: string): Promise<DeleteTeamNote>;
29
+ deleteTeamNote(teamPath: string, noteId: string): Promise<AxiosResponse>;
30
+ private unwrapData;
22
31
  }
32
+ export default API;
package/dist/index.js CHANGED
@@ -1,8 +1,12 @@
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
11
  constructor(accessToken, hackmdAPIEndpointURL = "https://api.hackmd.io/v1") {
8
12
  this.accessToken = accessToken;
@@ -36,55 +40,56 @@ class API {
36
40
  }
37
41
  });
38
42
  }
39
- async getMe() {
40
- const { data } = await this.axios.get("me");
41
- return data;
43
+ async getMe(options = defaultOption) {
44
+ return this.unwrapData(this.axios.get("me"), options.unwrapData);
42
45
  }
43
- async getHistory() {
44
- const { data } = await this.axios.get("history");
45
- return data;
46
+ async getHistory(options = defaultOption) {
47
+ return this.unwrapData(this.axios.get("history"), options.unwrapData);
46
48
  }
47
- async getNoteList() {
48
- const { data } = await this.axios.get("notes");
49
- return data;
49
+ async getNoteList(options = defaultOption) {
50
+ return this.unwrapData(this.axios.get("notes"), options.unwrapData);
50
51
  }
51
- async getNote(noteId) {
52
- const { data } = await this.axios.get(`notes/${noteId}`);
53
- return data;
52
+ async getNote(noteId, options = defaultOption) {
53
+ return this.unwrapData(this.axios.get(`notes/${noteId}`), options.unwrapData);
54
54
  }
55
- async createNote(options) {
56
- const { data } = await this.axios.post("notes", options);
57
- return data;
55
+ async createNote(payload, options = defaultOption) {
56
+ return this.unwrapData(this.axios.post("notes", payload), options.unwrapData);
58
57
  }
59
- async updateNoteContent(noteId, content) {
60
- await this.axios.patch(`notes/${noteId}`, { content });
58
+ async updateNoteContent(noteId, content, options = defaultOption) {
59
+ return this.unwrapData(this.axios.patch(`notes/${noteId}`, { content }), options.unwrapData);
61
60
  }
62
- async updateNote(noteId, options) {
63
- return await this.axios.patch(`notes/${noteId}`, options);
61
+ async updateNote(noteId, payload, options = defaultOption) {
62
+ return this.unwrapData(this.axios.patch(`notes/${noteId}`, payload), options.unwrapData);
64
63
  }
65
- async deleteNote(noteId) {
66
- await this.axios.delete(`notes/${noteId}`);
64
+ async deleteNote(noteId, options = defaultOption) {
65
+ return this.unwrapData(this.axios.delete(`notes/${noteId}`), options.unwrapData);
67
66
  }
68
- async getTeams() {
69
- const { data } = await this.axios.get("teams");
70
- return data;
67
+ async getTeams(options = defaultOption) {
68
+ return this.unwrapData(this.axios.get("teams"), options.unwrapData);
71
69
  }
72
- async getTeamNotes(teamPath) {
73
- const { data } = await this.axios.get(`teams/${teamPath}/notes`);
74
- return data;
70
+ async getTeamNotes(teamPath, options = defaultOption) {
71
+ return this.unwrapData(this.axios.get(`teams/${teamPath}/notes`), options.unwrapData);
75
72
  }
76
- async createTeamNote(teamPath, options) {
77
- const { data } = await this.axios.post(`teams/${teamPath}/notes`, options);
78
- return data;
73
+ async createTeamNote(teamPath, payload, options = defaultOption) {
74
+ return this.unwrapData(this.axios.post(`teams/${teamPath}/notes`, payload), options.unwrapData);
79
75
  }
80
76
  async updateTeamNoteContent(teamPath, noteId, content) {
81
- await this.axios.patch(`teams/${teamPath}/notes/${noteId}`, { content });
77
+ return this.axios.patch(`teams/${teamPath}/notes/${noteId}`, { content });
82
78
  }
83
79
  async updateTeamNote(teamPath, noteId, options) {
84
- return await this.axios.patch(`teams/${teamPath}/notes/${noteId}`, options);
80
+ return this.axios.patch(`teams/${teamPath}/notes/${noteId}`, options);
85
81
  }
86
82
  async deleteTeamNote(teamPath, noteId) {
87
- await this.axios.delete(`teams/${teamPath}/notes/${noteId}`);
83
+ return this.axios.delete(`teams/${teamPath}/notes/${noteId}`);
84
+ }
85
+ unwrapData(reqP, unwrap = true) {
86
+ if (unwrap) {
87
+ return reqP.then(response => response.data);
88
+ }
89
+ else {
90
+ return reqP;
91
+ }
88
92
  }
89
93
  }
94
+ exports.API = API;
90
95
  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.3.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;