Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update typings #579

Closed
wants to merge 2 commits into from
Closed

Update typings #579

wants to merge 2 commits into from

Conversation

IRus
Copy link

@IRus IRus commented Dec 6, 2016

Updating typings.

I used Typescript 2.x

  • Add generic type to both Promise and Response.
  • Delete typings since es6-promise package contains type definitions.

Example of usage before:

import axios, * as axiosTypes from 'axios';

export class RestHttpService {
  static Get<T>(path: string, config?: axiosTypes.AxiosRequestConfig): Promise<T> {
    return axios.get(path, config).then(payload => payload.data) as Promise<T>;
  };

  static Post<T>(path: string, data?: any, config?: axiosTypes.AxiosRequestConfig): Promise<T> {
    return axios.post(path, data, config).then(payload => payload.data) as Promise<T>;
  };

  static Put<T>(path: string, body?: any, config?: axiosTypes.AxiosRequestConfig): Promise<T> {
    return axios.put(path, body, config).then(payload => payload.data) as Promise<T>;
  };

  static Remove<T>(path: string, config?: axiosTypes.AxiosRequestConfig): Promise<T> {
    return axios.delete(path, config).then(payload => payload.data) as Promise<T>;
  };
}

Example of usage after:

import axios, {AxiosRequestConfig, AxiosResponse, Promise} from 'axios';

export class RestHttpService {
  static Get<T>(path: string, config?: AxiosRequestConfig): Promise<T> {
    return axios.get<T>(path, config).then(payload => payload.data);
  };

  static Post<T>(path: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
    return axios.post<T>(path, data, config);
  };

  static Put<T>(path: string, body?: any, config?: AxiosRequestConfig): Promise<T> {
    return axios.put<T>(path, body, config).then(payload => payload.data);
  };

  static Remove<T>(path: string, config?: AxiosRequestConfig): Promise<T> {
    return axios.delete<T>(path, config).then(payload => payload.data);
  };
}

See also #508

@coveralls
Copy link

Coverage Status

Coverage increased (+0.3%) to 93.909% when pulling 21db0e1 on IRus:master into cfe33d4 on mzabriskie:master.

@basarat
Copy link

basarat commented Dec 26, 2016

I have reviewed this and this is exactly what I was about to PR. looking forward to this getting released 🌹❤

@IRus
Copy link
Author

IRus commented Jan 3, 2017

@mzabriskie hi, something wrong with this PR? How can i help you to get this PR done? Happy Holidays! :)

@dpoineau
Copy link

dpoineau commented Jan 5, 2017

Is there a reason that these updated Typescript definitions (and the original ones) include their own re-declaration of the Promise<T> type, rather than just relying on the global (or polyfilled) type existing ( https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts#L5261 or https://github.com/DefinitelyTyped/DefinitelyTyped/blob/types-2.0/es6-promise/index.d.ts )

This can actually cause issues because you end up with two incompatible Promise types if you directly use the global/polyfilled type elsewhere, which can cause an error like was seen here: #480 (comment)

My assumption would be that there's no reason to re-declare Promise at all: for example, see how the fetch API typescript definitions just assume it's available: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/whatwg-fetch/index.d.ts

@IRus
Copy link
Author

IRus commented Jan 5, 2017

@dpoineau i think changing Promise declaration out of this PR scope

@IRus
Copy link
Author

IRus commented Jan 5, 2017

And since TypeScript can merge interfaces i doesn't see any problems here http://www.typescriptlang.org/docs/handbook/declaration-merging.html

@mktange
Copy link

mktange commented Jan 11, 2017

@dpoineau That issue is fixed with this PR: #588

@Lxxyx
Copy link

Lxxyx commented Feb 10, 2017

Generic type for Promise is important and useful for typescript users.

@IRus
Copy link
Author

IRus commented Feb 10, 2017

@mzabriskie, @rubennorte, @nickuraltsev any update on this really small and useful change?

@Lxxyx
Copy link

Lxxyx commented Feb 10, 2017

https://www.npmjs.com/package/@types/axios

There is an types package for axios with Generic type. It's useful and helpful for typescript users.
So anybody can update on this really small and useful change?

Or I will try to update @types/axios to newest version.

@IRus
Copy link
Author

IRus commented Feb 10, 2017

@Lxxyx this is always solution, but i hope maintainers will accept this PR. Two month from submitting 😕

@Lxxyx
Copy link

Lxxyx commented Feb 10, 2017

@IRus Yes, I'm ready to write the definition files by myself.

@morlay
Copy link

morlay commented Feb 18, 2017

export default axios should be export = axios

@IRus
Copy link
Author

IRus commented Feb 19, 2017

@morlay why? export default works well for me

@morlay
Copy link

morlay commented Feb 20, 2017

@IRus Sorry, I miss this line code module.exports.default = axios;

@IRus
Copy link
Author

IRus commented Mar 13, 2017

@mzabriskie, @rubennorte, @nickuraltsev Hi, just want to know is there any problems with this PR

@Kuirak
Copy link

Kuirak commented Apr 18, 2017

@mzabriskie, @rubennorte, @nickuraltsev can we merge and release this. It would make types so much better.

@djyde
Copy link

djyde commented Jul 27, 2017

Any progress? cc @nickuraltsev

@nickuraltsev
Copy link
Member

Closing in favor of #1061

@axios axios locked and limited conversation to collaborators May 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants