From 1835a92306d41e9a8956410ac9126952a00d8654 Mon Sep 17 00:00:00 2001 From: Marton Lederer Date: Tue, 21 Jul 2020 10:36:42 +0200 Subject: [PATCH] Simplify results, add instance TODO: fix instance --- lib/main.ts | 11 ++++++++++ lib/src/Requester.ts | 11 ++++++---- lib/types.ts | 10 ++++----- tests/mod_test.ts | 48 ++++++++++++++++++++++++++------------------ 4 files changed, 52 insertions(+), 28 deletions(-) diff --git a/lib/main.ts b/lib/main.ts index f27cade..b52f19d 100644 --- a/lib/main.ts +++ b/lib/main.ts @@ -24,5 +24,16 @@ summon.connect = async (url: string, data?: IData | string | FormData, config?: summon.trace = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'trace', data })) summon.patch = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'patch', data })) +// create an instace of summon +summon.create = (config?: IRequest) => { + + const summonInstance = Object.assign({}, summon) + + summonInstance.defaultConfig = Object.assign({}, summon.defaultConfig, config) // add a default config for the instance overriding the summon default with user specified one + + return summonInstance + +} + // the default config summon.defaultConfig = { method: 'get', timeout: 0, throwErrorOnTimeout: false, validateStatus: (status: number) => (status >= 200 && status < 300) } \ No newline at end of file diff --git a/lib/src/Requester.ts b/lib/src/Requester.ts index 21e5bd8..943af01 100644 --- a/lib/src/Requester.ts +++ b/lib/src/Requester.ts @@ -21,6 +21,9 @@ export async function doRequest ({ const Request: RequestInit = {} + if(url === undefined) + url = '/' + // using base url ? if(baseURL) if(url.includes('://')) @@ -127,16 +130,16 @@ export async function doRequest ({ if(validateStatus) validStatusCode = validateStatus(status) // if there is a validate function, use that if(validStatusCode) - return { response: { status, statusText, data: responseData, headers: responseHeaders, config: finalConfig }, error: null, res: { status, statusText, data: responseData, headers: responseHeaders, config: finalConfig }, err: null } + return { response: { status, statusText, data: responseData, headers: responseHeaders, config: finalConfig }, res: { status, statusText, data: responseData, headers: responseHeaders, config: finalConfig } } else - return { response: null, error: { response: { status, statusText, data: responseData, headers: responseHeaders }, config: finalConfig }, res: null, err: { response: { status, statusText, data: responseData, headers: responseHeaders }, config: finalConfig } } + return { error: { response: { status, statusText, data: responseData, headers: responseHeaders }, config: finalConfig }, err: { response: { status, statusText, data: responseData, headers: responseHeaders }, config: finalConfig } } }catch(error) { - return { response: null, error, res: null, err: error } + return { error, err: error } } - return { response: null, error: new Error('Unknown error'), res: null, err: new Error('Unknown error') } + return { error: new Error('Unknown error'), err: new Error('Unknown error') } } \ No newline at end of file diff --git a/lib/types.ts b/lib/types.ts index 126ba12..89481fe 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -1,6 +1,6 @@ export interface IRequest { - url: string + url?: string baseURL?: string method?: string | 'get' | 'post' | 'put' | 'delete' | 'options' | 'head' | 'connect' | 'trace' | 'patch' headers?: IHeadData @@ -40,10 +40,10 @@ export interface IParams { export interface IResult { - response: IResponse | null - error: Error | IResponseError | null - res: IResponse | null // shortcut - err: Error | IResponseError | null // shortcut + response?: IResponse | null + error?: Error | IResponseError | null + res?: IResponse | null // shortcut + err?: Error | IResponseError | null // shortcut } diff --git a/tests/mod_test.ts b/tests/mod_test.ts index 34a924c..72e98d5 100644 --- a/tests/mod_test.ts +++ b/tests/mod_test.ts @@ -16,13 +16,12 @@ Deno.test({ if(error) throw new Error(JSON.stringify(error)) + else if(response) { - if(response === null) - throw new Error('Response is null') + console.log(response) + assertEquals(response.data.id, 1) - console.log(response) - - assertEquals(response.data.id, 1) + } } @@ -54,13 +53,12 @@ Deno.test({ if(err) throw new Error(JSON.stringify(err)) + else if(res) { - if(res === null) - throw new Error('Response is null') - - console.log(res) + console.log(res) + assertEquals(res.data.title, 'foo') - assertEquals(res.data.title, 'foo') + } } @@ -80,11 +78,8 @@ Deno.test({ if(error) throw new Error(JSON.stringify(error)) - - if(response === null) - throw new Error('Response is null') - - console.log(response) + else if(response) + console.log(response) } @@ -99,12 +94,27 @@ Deno.test({ if(error) throw new Error(JSON.stringify(error)) + else if(response) + console.log(`Got response. The nest.land repo has ${ response.data.stargazers_count } starts, ${ response.data.forks_count } forks and is written in ${ response.data.language }.`) - if(response === null) - throw new Error('Response is null') + } - console.log(`Got response. The nest.land repo has ${ response.data.stargazers_count } starts, ${ response.data.forks_count } forks and is written in ${ response.data.language }.`) +}) - } +Deno.test({ + name: 'Test summon instance #1 - GET request\n', + async fn () { + + const INSTANCE = summon.create({ baseURL: 'https://api.github.com' }) + + let { response, error } = await INSTANCE.get('users/MartonDev') + + if(error) + throw new Error(JSON.stringify(error)) + else if(response) + console.log(`Marton Lederer has ${ response.data.public_repos } public repos`) + + } + }) \ No newline at end of file