Coder Social home page Coder Social logo

supercharge / sttp Goto Github PK

View Code? Open in Web Editor NEW
11.0 3.0 5.0 58 KB

An HTTP client for a pleasant developer experience.

License: MIT License

JavaScript 48.73% TypeScript 51.27%
javascript http http-client axios wrapper axios-wrapper supercharge hacktoberfest

sttp's Introduction



Sttp

An Axios wrapper for an enjoyable developer experience.


InstallationResources



Latest Version Monthly downloads

Follow @marcuspoehls and @superchargejs for updates!


Introduction

@supercharge/sttp is an elegant wrapper around the axios HTTP client. It provides a fluent interface allowing you to create HTTP requests communicating with other web applications.

Installation

npm i @supercharge/sttp

Resources

Contributing

Do you miss a string function? We very much appreciate your contribution! Please send in a pull request 馃槉

  1. Create a fork
  2. Create your feature branch: git checkout -b my-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request 馃殌

License

MIT 漏 Supercharge


superchargejs.com 聽路聽 GitHub @supercharge 聽路聽 Twitter @superchargejs

sttp's People

Contributors

godwinpinto avatar marcuspoehls avatar tiarebalbi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

sttp's Issues

Nedd axios.all() functionality

What you provide in sttp.axios() is an AxiosInstance not AxiosStatic.

export interface AxiosInstance extends Axios {
  (config: AxiosRequestConfig): AxiosPromise;
  (url: string, config?: AxiosRequestConfig): AxiosPromise;
}

export interface AxiosStatic extends AxiosInstance {
  create(config?: AxiosRequestConfig): AxiosInstance;
  Cancel: CancelStatic;
  CancelToken: CancelTokenStatic;
  Axios: typeof Axios;
  readonly VERSION: string;
  isCancel(value: any): boolean;
  all<T>(values: Array<T | Promise<T>>): Promise<T[]>;
  spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
  isAxiosError(payload: any): payload is AxiosError;
}

Therefore, I cant use axios.all()

axios.all(promises.map((promise) => axios.get(promise))).then(
  axios.spread((user, repos, followers, following) => {
    console.log({ user, repos, followers, following });
  })
);

:)

Bug: withQueryParams merging object instead of replacing

Doesn't matter how you calling the withQueryParams the this.request.params gets merged with previous value.

In the scenario where I had a filter and now I'm removing the filter the
object params was:

this.request.params = {
    status: 1
}

Now I want to remove, I will the call the withQueryParams({}).... the result of this operation will result in the same filter I had before.

I would suggest refactoring the following:
https://github.com/supercharge/sttp/blob/main/src/sttp.ts#L122-L133

to:

withQueryParams (queryParams: object): this {
  this.request.params = queryParams

  return this
}

@marcuspoehls thoughts?

My workaround was to manually override the setting

_.set(instance.requestConfig(), 'params', undefined);

To add statusText in SttpResponse

Description
While the library has option to get the SttpResponse status method, It would be good to have the statusText which is missing. This might help show the message for technical support perspective in addition to status, specially when users sent screenshot of problem.

Solution
Exposing the statusText method.

Thank you for the great library.

Exposing axios instance for test

Description:
During tests is hard to mock the HTTP requests created by the Sttp library because the axios instance is set as private. Would be possible to expose the axios instance in the Sttp class?

Example:

import MockAdapter from 'axios-mock-adapter';

describe('My Test', () => {
    it('should be able to authenticate successfully', async () => {
      const mock = new MockAdapter(Sttp.axios);

      mock.onPost('https://api.auth.com/oauth/token').reply(200, {
        access_token: '123',
        refresh_token: 'v1.321',
        id_token: '1234',
        scope: 'openid profile email address phone offline_access',
        expires_in: 86400,
        token_type: 'Bearer',
      });

      const response = await Sttp.withBasicAuth('demo', 'demo')
        .withBaseUrl('https://api.auth.com')
        .post('/oauth/token', {....});

      expect(response).toMatchSnapshot();
    });
  });

Reference:
Screenshot 2022-01-23 at 10 29 12

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    馃枛 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 馃搳馃搱馃帀

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google 鉂わ笍 Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.