Skip to content

Commit

Permalink
Fix instance
Browse files Browse the repository at this point in the history
  • Loading branch information
martonlederer committed Jul 21, 2020
1 parent 1835a92 commit 1f002e6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 30 deletions.
23 changes: 15 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@
You can also access all requests with their respective lowercase shortcuts

```ts
const { res, err } = await summon.post('https://example.com/post', { // you can also use shortcuts to response and error (res, err)
const { res } = await summon.post('https://example.com/post', { // you can also use shortcuts to response and error (res, err)

title: 'test',
description: 'This is a test description for a new post'

})
// or
const { res, err } = await summon.put(...)
const { res, err } = await summon.delete(...)
const { res, err } = await summon.patch(...)
const { res, err } = await summon.options(...)
const { res, err } = await summon.head(...)
const { res, err } = await summon.connect(...)
const { res, err } = await summon.trace(...)
const { res } = await summon.put(...)
const { res } = await summon.delete(...)
const { res } = await summon.patch(...)
const { res } = await summon.options(...)
const { res } = await summon.head(...)
const { res } = await summon.connect(...)
const { res } = await summon.trace(...)
```

## Summon API
Expand Down Expand Up @@ -79,6 +79,13 @@

})
```

### Instance
You can create an instance of summon with the `create()` function
```ts
const INSTANCE = summon.create({ baseURL: 'https://api.github.com/' })
```

# License
Licensed under the [MIT License](https://github.com/MartonDev/summon/blob/master/LICENSE)
Contributions are welcome.
20 changes: 11 additions & 9 deletions lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { doRequest } from './src/Requester.ts'
// summon API
export async function summon (urlOrConfig: string | IRequest, config?: IRequest): Promise<IResult> {

console.log(summon.defaultConfig);

if(config !== undefined && typeof urlOrConfig === 'string') // the url parameter is acutally used
return doRequest(Object.assign({}, summon.defaultConfig, { urlOrConfig }, config)) // overwriting the default values
else if(typeof urlOrConfig !== 'string') // the url parameter is not acutally used, but as a config
Expand All @@ -15,19 +17,19 @@ export async function summon (urlOrConfig: string | IRequest, config?: IRequest)

// shortcuts
summon.get = async (url: string, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'get' }))
summon.post = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'post', data }))
summon.delete = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'delete', data }))
summon.put = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'put', data }))
summon.options = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'options', data }))
summon.head = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'head', data }))
summon.connect = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, summon.defaultConfig, { url }, config, { method: 'connect', data }))
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 }))
summon.post = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'post', data }))
summon.delete = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'delete', data }))
summon.put = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'put', data }))
summon.options = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'options', data }))
summon.head = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'head', data }))
summon.connect = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'connect', data }))
summon.trace = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'trace', data }))
summon.patch = async (url: string, data?: IData | string | FormData, config?: IRequest): Promise<IResult> => doRequest(Object.assign({}, { url }, config, { method: 'patch', data }))

// create an instace of summon
summon.create = (config?: IRequest) => {

const summonInstance = Object.assign({}, summon)
const summonInstance = summon

summonInstance.defaultConfig = Object.assign({}, summon.defaultConfig, config) // add a default config for the instance overriding the summon default with user specified one

Expand Down
6 changes: 5 additions & 1 deletion lib/src/Requester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ export async function doRequest ({
url = '/'

// using base url ?
if(baseURL)
if(baseURL) {

if(url.includes('://'))
throw new Error('Base URL is already given, counting url as an URI. You cannot use a protocol with an URI.') // if there is a base url, we count the url field as an URI, so it cannot have a protocol
else
url = concatURL(baseURL, url) // concat base url with the URI string

}else if(!url.includes('://'))
throw new Error(`No base URL for URI ${ url }`)

// method exists ?
if(method) {
Expand Down
24 changes: 12 additions & 12 deletions tests/mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,35 @@ Deno.test({
})

Deno.test({
name: 'Test summon shortcuts #1 - GET request\n',

name: 'Test summon instance #1 - GET request\n',
async fn () {

let { response, error } = await summon.get('https://api.github.com/repos/nestdotland/nest.land')
const INSTANCE = summon.create({ baseURL: 'https://api.github.com/users' })

let { response, error } = await INSTANCE({ url: '/MartonDev' })

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 }.`)
console.log(`Marton Lederer has ${ response.data.public_repos } public repos`)

}

})

Deno.test({

name: 'Test summon instance #1 - GET request\n',
name: 'Test summon shortcuts #1 - GET request\n',
async fn () {

const INSTANCE = summon.create({ baseURL: 'https://api.github.com' })

let { response, error } = await INSTANCE.get('users/MartonDev')
let { response, error } = await summon.get('https://api.github.com/repos/nestdotland/nest.land')

if(error)
throw new Error(JSON.stringify(error))
else if(response)
console.log(`Marton Lederer has ${ response.data.public_repos } public repos`)
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 }.`)

}

})

0 comments on commit 1f002e6

Please sign in to comment.