|
1 | 1 | import { DeepgramApiError, DeepgramError, DeepgramUnknownError } from "../lib/errors"; |
2 | 2 | import { Readable } from "stream"; |
3 | 3 | import { fetchWithAuth, resolveResponse } from "../lib/fetch"; |
4 | | -import type { Fetch, FetchParameters, RequestMethodType } from "../lib/types/Fetch"; |
| 4 | +import type { Fetch, FetchOptions, RequestMethodType } from "../lib/types/Fetch"; |
5 | 5 | import { AbstractClient } from "./AbstractClient"; |
6 | 6 | import { DeepgramClientOptions } from "../lib/types"; |
7 | 7 | import { isBrowser } from "../lib/helpers"; |
| 8 | +import merge from "deepmerge"; |
8 | 9 |
|
9 | 10 | export abstract class AbstractRestClient extends AbstractClient { |
10 | 11 | protected fetch: Fetch; |
@@ -49,113 +50,85 @@ export abstract class AbstractRestClient extends AbstractClient { |
49 | 50 | } |
50 | 51 | } |
51 | 52 |
|
52 | | - protected _getRequestParams( |
| 53 | + protected _getRequestOptions( |
53 | 54 | method: RequestMethodType, |
54 | | - headers?: Record<string, string>, |
55 | | - parameters?: FetchParameters, |
56 | | - body?: string | Buffer | Readable |
57 | | - ) { |
58 | | - const params: { [k: string]: any } = { |
59 | | - ...this.options?.fetch, |
60 | | - method, |
61 | | - headers: { ...this.options?.fetch?.headers, ...headers } || {}, |
62 | | - }; |
63 | | - |
64 | | - if (method === "GET") { |
65 | | - return params; |
66 | | - } |
| 55 | + bodyOrOptions?: string | Buffer | Readable | FetchOptions, |
| 56 | + options?: FetchOptions |
| 57 | + ): FetchOptions { |
| 58 | + let reqOptions: FetchOptions = { method }; |
67 | 59 |
|
68 | | - params.body = body; |
69 | | - params.duplex = "half"; |
| 60 | + if (method === "GET" || method === "DELETE") { |
| 61 | + reqOptions = { ...reqOptions, ...(bodyOrOptions as FetchOptions) }; |
| 62 | + } else { |
| 63 | + reqOptions = { |
| 64 | + duplex: "half", |
| 65 | + body: bodyOrOptions as BodyInit, |
| 66 | + ...reqOptions, |
| 67 | + ...options, |
| 68 | + }; |
| 69 | + } |
70 | 70 |
|
71 | | - return { ...params, ...parameters }; |
| 71 | + return merge(this.namespaceOptions.fetch.options, reqOptions); |
72 | 72 | } |
73 | 73 |
|
74 | 74 | protected async _handleRequest( |
75 | | - fetcher: Fetch, |
| 75 | + method: "GET" | "DELETE", |
| 76 | + url: URL, |
| 77 | + options?: FetchOptions |
| 78 | + ): Promise<Response>; |
| 79 | + protected async _handleRequest( |
| 80 | + method: "POST" | "PUT" | "PATCH", |
| 81 | + url: URL, |
| 82 | + body: string | Buffer | Readable, |
| 83 | + options?: FetchOptions |
| 84 | + ): Promise<Response>; |
| 85 | + protected async _handleRequest( |
76 | 86 | method: RequestMethodType, |
77 | | - url: string | URL, |
78 | | - headers?: Record<string, string>, |
79 | | - parameters?: FetchParameters, |
80 | | - body?: string | Buffer | Readable |
81 | | - ): Promise<any> { |
| 87 | + url: URL, |
| 88 | + bodyOrOptions?: string | Buffer | Readable | FetchOptions, |
| 89 | + options?: FetchOptions |
| 90 | + ): Promise<Response> { |
82 | 91 | return new Promise((resolve, reject) => { |
83 | | - fetcher(url, this._getRequestParams(method, headers, parameters, body)) |
84 | | - .then((result) => { |
85 | | - if (!result.ok) throw result; |
86 | | - |
87 | | - return result.json(); |
88 | | - }) |
89 | | - .then((data) => resolve(data)) |
90 | | - .catch((error) => this._handleError(error, reject)); |
91 | | - }); |
92 | | - } |
| 92 | + const fetcher = this.fetch; |
93 | 93 |
|
94 | | - protected async _handleRawRequest( |
95 | | - fetcher: Fetch, |
96 | | - method: RequestMethodType, |
97 | | - url: string | URL, |
98 | | - headers?: Record<string, string>, |
99 | | - parameters?: FetchParameters, |
100 | | - body?: string | Buffer | Readable |
101 | | - ): Promise<any> { |
102 | | - return new Promise((resolve, reject) => { |
103 | | - fetcher(url, this._getRequestParams(method, headers, parameters, body)) |
| 94 | + fetcher(url, this._getRequestOptions(method, bodyOrOptions, options)) |
104 | 95 | .then((result) => { |
105 | 96 | if (!result.ok) throw result; |
106 | | - |
107 | | - return result; |
| 97 | + resolve(result); |
108 | 98 | }) |
109 | | - .then((data) => resolve(data)) |
110 | 99 | .catch((error) => this._handleError(error, reject)); |
111 | 100 | }); |
112 | 101 | } |
113 | 102 |
|
114 | | - protected async get( |
115 | | - fetcher: Fetch, |
116 | | - url: string | URL, |
117 | | - headers?: Record<string, string>, |
118 | | - parameters?: FetchParameters |
119 | | - ): Promise<any> { |
120 | | - return this._handleRequest(fetcher, "GET", url, headers, parameters); |
| 103 | + protected async get(url: URL, options?: FetchOptions): Promise<any> { |
| 104 | + return this._handleRequest("GET", url, options); |
121 | 105 | } |
122 | 106 |
|
123 | 107 | protected async post( |
124 | | - fetcher: Fetch, |
125 | | - url: string | URL, |
| 108 | + url: URL, |
126 | 109 | body: string | Buffer | Readable, |
127 | | - headers?: Record<string, string>, |
128 | | - parameters?: FetchParameters |
| 110 | + options?: FetchOptions |
129 | 111 | ): Promise<any> { |
130 | | - return this._handleRequest(fetcher, "POST", url, headers, parameters, body); |
| 112 | + return this._handleRequest("POST", url, body, options); |
131 | 113 | } |
132 | 114 |
|
133 | 115 | protected async put( |
134 | | - fetcher: Fetch, |
135 | | - url: string | URL, |
| 116 | + url: URL, |
136 | 117 | body: string | Buffer | Readable, |
137 | | - headers?: Record<string, string>, |
138 | | - parameters?: FetchParameters |
| 118 | + options?: FetchOptions |
139 | 119 | ): Promise<any> { |
140 | | - return this._handleRequest(fetcher, "PUT", url, headers, parameters, body); |
| 120 | + return this._handleRequest("PUT", url, body, options); |
141 | 121 | } |
142 | 122 |
|
143 | 123 | protected async patch( |
144 | | - fetcher: Fetch, |
145 | | - url: string | URL, |
| 124 | + url: URL, |
146 | 125 | body: string | Buffer | Readable, |
147 | | - headers?: Record<string, string>, |
148 | | - parameters?: FetchParameters |
| 126 | + options?: FetchOptions |
149 | 127 | ): Promise<any> { |
150 | | - return this._handleRequest(fetcher, "PATCH", url, headers, parameters, body); |
| 128 | + return this._handleRequest("PATCH", url, body, options); |
151 | 129 | } |
152 | 130 |
|
153 | | - protected async delete( |
154 | | - fetcher: Fetch, |
155 | | - url: string | URL, |
156 | | - headers?: Record<string, string>, |
157 | | - parameters?: FetchParameters |
158 | | - ): Promise<any> { |
159 | | - return this._handleRequest(fetcher, "DELETE", url, headers, parameters); |
| 131 | + protected async delete(url: URL, options?: FetchOptions): Promise<any> { |
| 132 | + return this._handleRequest("DELETE", url, options); |
160 | 133 | } |
161 | 134 | } |
0 commit comments