|
| 1 | +import { |
| 2 | + GetFilesResponse, |
| 3 | + ENDPOINTS, |
| 4 | + DocumentReadResponse, |
| 5 | + SuccessfulResponse |
| 6 | +} from "./api"; |
| 7 | +import { Dynalist } from "./dynalist"; |
| 8 | +import fetch from "node-fetch"; |
| 9 | + |
| 10 | +interface FetchRequest { |
| 11 | + url: string; |
| 12 | + params?: any; |
| 13 | +} |
| 14 | + |
| 15 | +export class DynalistAPI { |
| 16 | + private token: string; |
| 17 | + // private fileTree: Dynalist.File[] | undefined; |
| 18 | + // private cache; // TODO |
| 19 | + |
| 20 | + constructor(token: string) { |
| 21 | + this.token = token; |
| 22 | + } |
| 23 | + |
| 24 | + // Limit: 60 times per minute |
| 25 | + public async readAllDocuments(): Promise<Dynalist.Document[]> { |
| 26 | + return this.getFileTree() |
| 27 | + .then( |
| 28 | + (files): Promise<DocumentReadResponse[]> => { |
| 29 | + const requests = files.map( |
| 30 | + (file): FetchRequest => ({ |
| 31 | + url: ENDPOINTS.READ_DOCUMENT, |
| 32 | + params: { |
| 33 | + file_id: file.id |
| 34 | + } |
| 35 | + }) |
| 36 | + ); |
| 37 | + return this.batchPostFetch<DocumentReadResponse>(requests, 1500); |
| 38 | + } |
| 39 | + ) |
| 40 | + .then(responses => |
| 41 | + responses.map(response => { |
| 42 | + return { |
| 43 | + id: "abc", |
| 44 | + title: response.title, |
| 45 | + nodes: response.nodes |
| 46 | + }; |
| 47 | + }) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public readDocument(documentId: string): Promise<Dynalist.Document> { |
| 52 | + return this.postFetch(ENDPOINTS.READ_DOCUMENT, { |
| 53 | + file_id: documentId |
| 54 | + }).then((body: DocumentReadResponse) => ({ |
| 55 | + id: documentId, |
| 56 | + nodes: body.nodes, |
| 57 | + title: body.title |
| 58 | + })); |
| 59 | + } |
| 60 | + |
| 61 | + public getFileTree(): Promise<Dynalist.File[]> { |
| 62 | + return this.postFetch<GetFilesResponse>(ENDPOINTS.GET_FILES).then( |
| 63 | + body => body.files |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + private async batchPostFetch<T extends SuccessfulResponse>( |
| 68 | + requests: FetchRequest[], |
| 69 | + waitMilliseconds: number |
| 70 | + ): Promise<T[]> { |
| 71 | + const results: T[] = []; |
| 72 | + for (const request of requests) { |
| 73 | + results.push(await this.postFetch<T>(request.url, request.params)); |
| 74 | + await new Promise(_ => setTimeout(_, waitMilliseconds)); |
| 75 | + } |
| 76 | + return results; |
| 77 | + } |
| 78 | + |
| 79 | + private postFetch<T extends SuccessfulResponse>( |
| 80 | + url: string, |
| 81 | + params?: any |
| 82 | + ): Promise<T> { |
| 83 | + return fetch(url, { |
| 84 | + headers: { |
| 85 | + "Content-Type": "application/json" |
| 86 | + }, |
| 87 | + body: JSON.stringify({ token: this.token, ...params }), |
| 88 | + method: "POST" |
| 89 | + }) |
| 90 | + .then(response => response.json()) |
| 91 | + .then(body => { |
| 92 | + if (body._code !== "Ok") return Promise.reject(body._msg); |
| 93 | + else return body as T; |
| 94 | + }); |
| 95 | + } |
| 96 | +} |
0 commit comments