SkillAgentSearch skills...

Soxa

Promise based HTTP client for the deno

Install / Use

/learn @fakoua/Soxa

README

soxa

GitHub code size in bytes GitHub GitHub last commit GitHub Workflow Status

Promise based HTTP client for deno

Table of Contents

Features

  • Make fetch requests from deno
  • Supports the Promise API
  • Intercept request and response
  • Transform request and response data
  • Cancel requests
  • Automatic transforms for JSON data

Installation

Using deno:

import { soxa } from 'https://deno.land/x/soxa/mod.ts'

Example

Performing a GET request (Promise)

import { soxa } from 'https://deno.land/x/soxa/mod.ts'

// soxa.get(url, config)
// soxa.head(url, config)
// soxa.delete(url, config)

// soxa.post(url, data, config)
// soxa.put(url, data, config)
// soxa.patch(url, data, config)


//Example
// Make a request for todos
soxa.get('https://jsonplaceholder.typicode.com/todos/1')
  .then(function (response) {
    // handle success
    console.log(response.data);
  })
  .catch(function (error) {
    // handle error
    console.log(error);
  })
  .finally(function () {
    // always executed
  });

Performing a GET request (Await/Async)

let result = await soxa.get('https://jsonplaceholder.typicode.com/todos/1');
console.log(result.data)

Performing a POST request

let response = await soxa.post('https://jsonplaceholder.typicode.com/posts', {
                          "title": "Hello Soxa",
                          "id": 14
                      });
//Note: data is passed with valid JSON format ( {"key": "string-value", "key2": int-value ...} )

//OR you can send the data with the config object.
let response = await soxa.post('https://jsonplaceholder.typicode.com/posts', {}, {
    headers: {'x-user': 'fakoua'},
    data: {
        "title":"Hello Soxa",
        "id":14
    }
});

URL Examples

await soxa.get('http://example.com'); // http://example.com
await soxa.get('http://example.com', { params: { q: 'hello' } }); // http://example.com?q=hello
await soxa.get('http://example.com', { params: { q: 'hello', id: 12 } }); // http://example.com?q=hello&id=12

await soxa.get('http://example.com/folder', { params: { q: 'hello' } }); // http://example.com/folder?q=hello

//Note: if baseURL is set in the config, you only need to pass the /folder relative path.
let config = {
  baseURL: 'http://example.com/',
  params: {
            q: 'hello'
          }
 }
await soxa.get('/folder', config); // http://example.com/folder?q=hello

Config

These are the available config options for making requests. Only the url is required. Requests will default to GET if method is not specified.

{
  // `baseURL` will be prepended to `url` unless `url` is absolute.
  // It can be convenient to set `baseURL` for an instance of soxa to pass relative URLs
  // to methods of that instance.
  baseURL: 'https://some-domain.com/api/',

  // `transformRequest` allows changes to the request data before it is sent to the server
  // This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'
  // The last function in the array must return a string or an instance of Buffer, ArrayBuffer,
  // FormData or Stream
  // You may modify the headers object.
  transformRequest: [function (data, headers) {
    // Do whatever you want to transform the data

    return data;
  }],

  // `transformResponse` allows changes to the response data to be made before
  // it is passed to then/catch
  transformResponse: [function (data) {
    // Do whatever you want to transform the data

    return data;
  }],

  // `headers` are custom headers to be sent
  headers: {'X-Requested-With': 'XMLHttpRequest'},

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  // Result:  [url]/?ID=12345
  params: {
    ID: 12345
  },

  // `paramsSerializer` is an optional function in charge of serializing `params`
  // (e.g. http://api.jquery.com/jquery.param/)
  paramsSerializer: function (params) {
    return ...
  },

  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // Browser only: FormData, File, Blob
  data: {
    firstName: 'Fred'
  },
  
  // syntax alternative to send data into the body
  // method post
  // only the value is sent, not the key
  data: 'Country=Brasil&City=Belo Horizonte',

  // `timeout` specifies the number of milliseconds before the request times out.
  // If the request takes longer than `timeout`, the request will be aborted.
  timeout: 1000, // default is `0` (no timeout)

  // `withCredentials` indicates whether or not cross-site Access-Control requests
  // should be made using credentials
  withCredentials: false, // default

  // `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
  // This will set an `Authorization` header, overwriting any existing
  // `Authorization` custom headers you have set using `headers`.
  // Please note that only HTTP Basic auth is configurable through this parameter.
  // For Bearer tokens and such, use `Authorization` custom headers instead.
  auth: {
    username: 'sam',
    password: 'pass'
  }, //This will be transformed and added to header -> "Authorization": "Basic c2FtOnBhc3M="

  // `responseType` indicates the type of data that the server will respond with
  // options are: 'arraybuffer', 'document', 'json', 'text', 'stream'
  //   browser only: 'blob'
  responseType: 'json', // default

  // `responseEncoding` indicates encoding to use for decoding responses
  // Note: Ignored for `responseType` of 'stream' or client-side requests
  responseEncoding: 'utf8', // default

  // `onUploadProgress` allows handling of progress events for uploads
  onUploadProgress: function (progressEvent) {
    // Do whatever you want with the native progress event
  },

  // `onDownloadProgress` allows handling of progress events for downloads
  onDownloadProgress: function (progressEvent) {
    // Do whatever you want with the native progress event
  },

  // `maxContentLength` defines the max size of the http response content in bytes allowed
  maxContentLength: 2000,

  // `validateStatus` defines whether to resolve or reject the promise for a given
  // HTTP response status code. If `validateStatus` returns `true` (or is set to `null`
  // or `undefined`), the promise will be resolved; otherwise, the promise will be
  // rejected.
  validateStatus: function (status) {
    return status >= 200 && status < 300; // default
  },

  // `maxRedirects` defines the maximum number of redirects to follow in node.js.
  // If set to 0, no redirects will be followed.
  maxRedirects: 5, // default

  // `socketPath` defines a UNIX Socket to be used in node.js.
  // e.g. '/var/run/docker.sock' to send requests to the docker daemon.
  // Only either `socketPath` or `proxy` can be specified.
  // If both are specified, `socketPath` is used.
  socketPath: null, // default

  // `cancelToken` specifies a cancel token that can be used to cancel the request
  // (see Cancellation section below for details)
  cancelToken: new CancelToken(function (cancel) {
  })
}

//Example auth:

let config = {
    auth: {
        username: 'myUser',
        password: 'myPassword'
      }
}

soxa.post(url, {} ,config)
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

Response Schema

The response for a request contains the following information.

{
  // `data` is the response that was provided by the server
  data: {},

  // `status` is the HTTP status code from the server response
  status: 200,

  // `statusText` is the HTTP status message from the server response
  statusText: 'OK',

  // `headers` the headers that the server responded with
  // All header names are lower cased
  headers: {},

  // `config` is the config that was provided to `soxa` for the request
  config: {},

  // `request` is the request that generated this response
  // It is the last ClientRequest instance in node.js (in redirects)
  // and an XMLHttpRequest instance in the browser
  request: {}
}

When using then, you will receive the response as follows:

soxa.get('/user/12345')
  .then(function (response) {
    console.log(response.data);
    console.log(response.status);
    console.log(response.statusText);
    console.log(response.headers);
    console.log(response.config);
  });

When using catch, or passing a rejection callback as second parameter of then, the response will be available through the error object as explained in the [Handling

Related Skills

View on GitHub
GitHub Stars35
CategoryDevelopment
Updated2y ago
Forks5

Languages

TypeScript

Security Score

80/100

Audited on Jun 19, 2023

No findings