|
| 1 | +import nodeFetch from 'node-fetch'; |
| 2 | +import * as querystring from 'querystring'; |
| 3 | + |
| 4 | +export class Gateway { |
| 5 | + |
| 6 | + /** ユーザーのアクセストークンです */ |
| 7 | + private token = ''; |
| 8 | + |
| 9 | + /** Qiitaのホストです */ |
| 10 | + private endpoint = 'https://qiita.com'; |
| 11 | + |
| 12 | + /** APIバージョンを示すロケーションです */ |
| 13 | + private version = '/api/v2'; |
| 14 | + |
| 15 | + /** |
| 16 | + * @param options Optional params |
| 17 | + * @param options.url Rest API URL of the instance |
| 18 | + * @param options.streamingUrl Streaming API URL of the instance |
| 19 | + * @param options.token API token of the user |
| 20 | + */ |
| 21 | + constructor (options: { url?: string, streamingUrl?: string, token?: string }) { |
| 22 | + if (options) { |
| 23 | + this.url = options.url || ''; |
| 24 | + this.streamingUrl = options.streamingUrl || ''; |
| 25 | + |
| 26 | + if (options.token) { |
| 27 | + this.token = options.token; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Fetch API wrapper function |
| 34 | + * @param url URL to request |
| 35 | + * @param options Fetch API options |
| 36 | + * @param parse Whether parse response before return |
| 37 | + * @return Parsed response object |
| 38 | + */ |
| 39 | + protected async request (url: string, options: { [key: string]: any } = {}, parse = true): Promise<any> { |
| 40 | + if ( !options.headers ) { |
| 41 | + options.headers = {}; |
| 42 | + } |
| 43 | + |
| 44 | + options.headers['Content-Type'] = 'application/json'; |
| 45 | + |
| 46 | + if ( !this.url ) { |
| 47 | + throw new MastodonURLResolveError('REST API URL has not been specified, Use Mastodon.setUrl to set your instance\'s URL'); |
| 48 | + } |
| 49 | + |
| 50 | + if ( this.token ) { |
| 51 | + options.headers.Authorization = `Bearer ${this.token}`; |
| 52 | + } |
| 53 | + |
| 54 | + const response = typeof window === 'undefined' |
| 55 | + ? await nodeFetch(url, options) |
| 56 | + : await fetch(url, options); |
| 57 | + |
| 58 | + if ( !parse ) { |
| 59 | + return response; |
| 60 | + } |
| 61 | + |
| 62 | + const data = await response.json(); |
| 63 | + |
| 64 | + if (response.ok) { |
| 65 | + return data; |
| 66 | + } else { |
| 67 | + switch (response.status) { |
| 68 | + case 401: |
| 69 | + throw new MastodonUnauthorizedError(data.error); |
| 70 | + case 404: |
| 71 | + throw new MastodonNotFoundError(data.error); |
| 72 | + case 429: |
| 73 | + throw new MastodonRatelimitError(data.error); |
| 74 | + default: |
| 75 | + throw new MastodonError('MastodonError', data.error || 'Unexpected error occurred'); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * HTTP GET |
| 82 | + * @param url URL to request |
| 83 | + * @param params Query strings |
| 84 | + * @param options Fetch API options |
| 85 | + * @param parse Whether parse response before return |
| 86 | + */ |
| 87 | + protected get <T> (url: string, params = {}, options = {}, parse = true): Promise<T> { |
| 88 | + return this.request(url + (Object.keys(params).length ? '?' + querystring.stringify(params) : ''), { method: 'GET', ...options }, parse); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * HTTP POST |
| 93 | + * @param url URL to request |
| 94 | + * @param body Payload |
| 95 | + * @param options Fetch API options |
| 96 | + * @param parse Whether parse response before return |
| 97 | + */ |
| 98 | + protected post <T> (url: string, body = {}, options = {}, parse = true): Promise<T> { |
| 99 | + return this.request(url, { method: 'POST', body: JSON.stringify(body), ...options }, parse); |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * HTTP PUT |
| 104 | + * @param url URL to request |
| 105 | + * @param body Payload |
| 106 | + * @param options Fetch API options |
| 107 | + * @param parse Whether parse response before return |
| 108 | + */ |
| 109 | + protected put <T> (url: string, body = {}, options = {}, parse = true): Promise<T> { |
| 110 | + return this.request(url, { method: 'PUT', body: JSON.stringify(body), ...options }, parse); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * HTTP DELETE |
| 115 | + * @param url URL to request |
| 116 | + * @param body Payload |
| 117 | + * @param options Fetch API options |
| 118 | + * @param parse Whether parse response before return |
| 119 | + */ |
| 120 | + protected delete <T> (url: string, body = {}, options = {}, parse = true): Promise<T> { |
| 121 | + return this.request(url, { method: 'DELETE', body: JSON.stringify(body), ...options }, parse); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * HTTP PATCH |
| 126 | + * @param url URL to request |
| 127 | + * @param body Payload |
| 128 | + * @param options Fetch API options |
| 129 | + * @param parse Whether parse response before return |
| 130 | + */ |
| 131 | + protected patch <T> (url: string, body = {}, options = {}, parse = true): Promise<T> { |
| 132 | + return this.request(url, { method: 'PATCH', body: JSON.stringify(body), ...options }, parse); |
| 133 | + } |
| 134 | +} |
0 commit comments