summon

nest badge summon build

An elegant requests library for Deno, modelled after axios

What is summon?

Summon provides an elegant, more readable interface to the Deno fetch API for making requests. It's codebase and layout is modelled after axios, but is more lightweight and uses a golang inspired return sytax.

Usage

First import the module from nest.land:

import { summon } from 'https://x.nest.land/summon@0.0.1/mod.ts'

A simple summon GET request looks like this:

const { response, error } = await summon.get('https://example.com', { params: {

  page: 1,
  testparam: true

}}) // get request with custom config

You can also access all requests with their respective lowercase shortcuts

const { response, error } = await summon.post('https://example.com/post', {

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

})
// or
const { response, error } = await summon.put(...)
const { response, error } = await summon.delete(...)
const { response, error } = await summon.patch(...)
const { response, error } = await summon.options(...)
const { response, error } = await summon.head(...)
const { response, error } = await summon.connect(...)
const { response, error } = await summon.trace(...)

Summon API

Like axios, summon has it's own API for maximum configuration and readability

const { response, error } = await summon('/test', {

 baseURL: 'https://example.com', // the base URL of the request. If this is not undefined, the library will concat the url to the value of this field. In this request, the baseURL and the url will be concated to https://example.com/test
 method: 'post', // 'get' | 'post' | 'put' | 'delete' | 'options' | 'head' | 'connect' | 'trace' | 'patch'
 headers: {

   'Accept': 'application/xml', // accept only xml response
   'Content-Type': 'application/json'

 },
 params: {

   page: 2 // this will be concated to "?page=2" and added to the request url

 },
 validateStatus: (status) => (status >= 200 && status < 210), // validate the returned status. In this example, if the status code is greater than 210, the result gives an error
 timeout: 100, // after how many milliseconds should the request timeout. Default is 0
 throwErrorOnTimeout: true // weather the library should throw an error on timeout (this will terminate your app, because of the way Deno behaves) Default: false
 auth: { username: 'test', password: 'password' }, // submit a base64 encoded auth request
 data: { somevalue: true }, // the request body 
 abortController: // an AbortController object that you can use to cancel/abort your request

})

You can even put the URL inside the config object

const { response, error } = await summon({

 url: 'https://example.com/',
 ...

})

License

Licensed under the MIT License Contributions are welcome.