npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mesalva/api

v2.4.9

Published

Lib para realizar os requests para a API de qualquer aplicação javascript, implementando HMAC e Serializer

Downloads

60

Readme

npm package "@mesalva/api"

npm version Build Status Maintainability Test Coverage

Introduction

This package is used by Me Salva Engineering Team to make requests to Api, using HMAC authentication protocol + universal fetch to make requests by server side (we use express) + Json Api Serializer to parse the api JSON API BASED to simple Javascript Camel Case Based objects

Installation

yarn way

yarn add @mesalva/api

npm way

npm install --save @mesalva/api

Configurations

.env file

On project root folder add a file called .env.js with the structure below:

module.exports = {
  API_HOST: 'https://API_HOST',
  CLIENT: 'WEB|ANDROID|IOS|...',
  HMAC_KEY: 'HMAC_KEY',
}

Usage

Creating a model

./examples/ExampleModelA.js

import MSApiRequest from '@mesalva/api'

const ExampleA = new MSApiRequest('some_api_route')

export default ExampleA

This model allow you to use the methods below:

./examples/ExampleUsage1.js

import Example1 from './examples/ExampleModelA'

Example1.request({
  method: 'GET|POST|PUT|DELETE|PATCH', //default = GET
  route: 'additional route', //default = GET
  data: {...OBJECT_WITH_REQUEST_PARAMS}, //default = undefined
  headers: {...OBJECT_WITH_ADDICTIONAL_HEADERS}, // default = undefined
  only: 'data|headers|status|meta', // default = undefined
}).then(response => {
  console.log(response.data)
  console.log(response.meta)
  console.log(response.headers)
  console.log(response.status)
})

Custom ENVS

You can pass some custom env vars on MsApiRequest initialization. This will allow to use two or more api accesses in the same project.

import MSApiRequest from '@mesalva/api'

const ExampleA = new MSApiRequest('some_api_route', {
  API_HOST: 'https://some.another.host',
  HMAC_KEY: 'SomeAnotherHmacKey'
})

export default ExampleA

Some alias

./examples/ExampleUsage2.js

import Example2 from './examples/ExampleModelA'

Example2.get().then(data => console.log(data))
// will make a GET request to: https://api_host/some_api_route

// is the same that:
Example2.request({ method: 'GET', only: 'data' }).then(data => console.log(data))
// or the same that:
Example2.request({ method: 'GET' }).then(response => console.log(response.data))


Example2.post({ data: {someField: 'someValue'}}).then(data => console.log(data))
// is the same that:
Example2.request({
  method: 'POST',
  only: 'data',
  data: {someField: 'someValue'},
}).then(data => console.log(data))
// will make a POST request to: https://api_host/some_api_route, sending as payload  {"someField": "someValue'}

These are the valid alias: [ post, put, delete, get, patch ]

Explaining request params

  • method - Set request HTTP method, the allow methods are: GET, POST, PUT, PATCH, DELETE.

  • data - For GET method, means that will add as a url query string params, to other methods send as payload in JSON hyphen-saparated format

  • headers - add headers to request. This lib will automatic add the headers below, so, this headers can't be overflowed.

    • X-DATE - Some enviroments don't allow to send DATE header, so we send this header by security

    • CLIENT - the same client provided on .env.js file

    • CONTENT-TYPE - application/json

    • PLATFORM - Send to api which platform is requesting

    • DEVICE - Send to api which device is requesting

    • USER-AGENT - Send to api the user-agent of the device that is requesting

    • CONTENT-MD5 - Encrypted data, using HMAC KEY

    • AUTHORIZATION - HMAC authentication protocol implementation created using the canonical string composed by: method + content type + Content-MD5 + pathname + date

    • UID - user authenticated uid (if is not guest user)

    • ACCESS-TOKEN - user authenticated access-token (if is not guest user)

Code Quality

The project have 100% test coverage and no lint issues! All master interactions must have the same code quality Coverage