Skip to content

Commit

Permalink
Simplify results, add instance
Browse files Browse the repository at this point in the history
TODO: fix instance
  • Loading branch information
martonlederer committed Jul 21, 2020
1 parent eccdacb commit 1835a92
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 28 deletions.
11 changes: 11 additions & 0 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<IResult> => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'trace', data }))
summon.patch = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => 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) }
11 changes: 7 additions & 4 deletions lib/src/Requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export async function doRequest ({

const Request: RequestInit = {}

if(url === undefined)
url = '/'

// using base url ?
if(baseURL)
if(url.includes('://'))
Expand Down Expand Up @@ -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') }

}
10 changes: 5 additions & 5 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

}

Expand Down
48 changes: 29 additions & 19 deletions tests/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

}

Expand Down Expand Up @@ -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')
}

}

Expand All @@ -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)

}

Expand All @@ -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`)

}

})

0 comments on commit 1835a92

Please sign in to comment.