1
1
import { RequestInit } from 'node-fetch' ;
2
- import { createServiceError } from '../core/sdk-exceptions' ;
2
+
3
3
import { fetch } from './fetch' ;
4
+ import { createServiceError } from '../core/sdk-exceptions' ;
4
5
5
- interface MagicAPIResponse < TData = { } > {
6
+ interface MagicAPIResponse < TData > {
6
7
data ?: TData ;
7
8
error_code ?: string ;
8
9
message ?: string ;
@@ -12,10 +13,10 @@ interface MagicAPIResponse<TData = {}> {
12
13
/**
13
14
* Performs a `fetch` to the given URL with the configured `init` object.
14
15
*/
15
- async function emitRequest < TResponse extends any = { } > ( url : string , init ?: RequestInit ) : Promise < Partial < TResponse > > {
16
+ async function emitRequest < TResponse > ( url : string , init ?: RequestInit ) : Promise < Partial < TResponse > > {
16
17
const json : MagicAPIResponse < TResponse > = await fetch ( url , init )
17
- . then ( res => res . json ( ) )
18
- . catch ( err => {
18
+ . then ( ( res ) => res . json ( ) )
19
+ . catch ( ( err ) => {
19
20
throw createServiceError ( err ) ;
20
21
} ) ;
21
22
@@ -29,7 +30,7 @@ async function emitRequest<TResponse extends any = {}>(url: string, init?: Reque
29
30
/**
30
31
* Generates an encoded URL with query string from a dictionary of values.
31
32
*/
32
- function generateQuery < T extends Record < string , string | number | boolean > = { } > ( url : string , params ?: T ) {
33
+ function generateQuery < T extends Record < string , string | number | boolean > > ( url : string , params ?: T ) {
33
34
let query = '?' ;
34
35
if ( params ) {
35
36
for ( const [ key , value ] of Object . entries ( params ) ) query += `${ key } =${ value } &` ;
@@ -41,7 +42,7 @@ function generateQuery<T extends Record<string, string | number | boolean> = {}>
41
42
/**
42
43
* POSTs to Magic's API.
43
44
*/
44
- export function post < TBody extends Record < string , string | number | boolean > = { } , TResponse extends any = { } > (
45
+ export function post < TBody extends Record < string , string | number | boolean > , TResponse > (
45
46
url : string ,
46
47
secretApiKey : string ,
47
48
body : TBody ,
@@ -56,7 +57,7 @@ export function post<TBody extends Record<string, string | number | boolean> = {
56
57
/**
57
58
* GETs from Magic's API.
58
59
*/
59
- export function get < TResponse extends any = { } > ( url : string , secretApiKey : string , params ?: any ) {
60
+ export function get < TResponse > ( url : string , secretApiKey : string , params ?: any ) {
60
61
const urlWithParams = generateQuery ( url , params ) ;
61
62
return emitRequest < TResponse > ( urlWithParams , {
62
63
method : 'GET' ,
0 commit comments