项目作者: FriendsOfReact

项目描述 :
HTTP client library with Axios and JavaScript decorators.
高级语言: JavaScript
项目地址: git://github.com/FriendsOfReact/axios-decorators.git
创建时间: 2020-02-09T14:10:49Z
项目社区:https://github.com/FriendsOfReact/axios-decorators

开源协议:MIT License

下载


Axios Decorators

JavaScript Style Guide
Quality Gate Status
npm

It is an HTTP client library that uses Axios and JavaScript decorators. It is inspired by the popular Android library Retrofit.



Dependencies

The main dependency is Babel, it uses decorators and classes to make JavaScript developers’ life easier. Hence there are two dependencies as plugin-proposal-decorators and plugin-proposal-class-properties.

Axios and Regenerator are other dependencies, you can find dependency installation in the following section.

Installation

Babel Core

First of all, Babel has to be installed, but if you have already Babel on your project, you can skip this part. Babel installation can differ by application. Please check the Babel’s documentation for detailed information.

Babel Plugins

  1. npm install --save-dev @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators

After plugin installation, following configuration have to be added to .babelrc file.

  1. {
  2. "presets": [...],
  3. "plugins": [
  4. [
  5. "@babel/plugin-proposal-decorators",
  6. {"legacy": true}
  7. ],
  8. ["@babel/plugin-proposal-class-properties"]
  9. ]
  10. }

Runtime Regenerator

If you already use @babel/polyfill, you can skip this part. However, @babel/polyfill has been deprecated, upgrading the project to core-js and regenerator is suggested.

  1. npm install --save regenerator

Flow

Flow is suggested to get auto-suggestion support on the IDE.

  1. npm install --save-dev @babel/core @babel/cli @babel/preset-flow

After installation, Flow’s preset has to be added to .babelrc file.

  1. {
  2. "presets": ["@babel/preset-flow"]
  3. }

Axios & Axios Decorators

  1. npm install --save axios axios-decorators

Usage Examples

Basic

You can access the basic example’s code from this link.

  1. // @flow
  2. import { AxiosPromise } from 'axios'
  3. import { DELETE, GET, PATCH, POST, PUT, Client } from 'axios-decorators'
  4. type PostType = {
  5. userId: number;
  6. id: number;
  7. title: string;
  8. body: string;
  9. }
  10. @Client({ baseURL: 'https://jsonplaceholder.typicode.com' })
  11. class PostClient {
  12. @GET('/posts')
  13. static getPostList (): AxiosPromise<PostType> {}
  14. @GET('/posts/:id')
  15. static getPost (args: { path: { id: string } }): AxiosPromise<PostType> {}
  16. @POST('/posts')
  17. static createPost (args: { data: PostType }): AxiosPromise<PostType> {}
  18. @PUT('/posts/:id')
  19. static putPost (args: { path: { id: string }, data: PostType }): AxiosPromise<PostType> {}
  20. @PATCH('/posts/:id')
  21. static patchPost (args: { path: { id: string }, data: PostType }): AxiosPromise<PostType> {}
  22. @DELETE('/posts/:id')
  23. static deletePost (args: { path: { id: string } }): AxiosPromise<PostType> {}
  24. }
  25. (async () => {
  26. // Get post list
  27. const postList = await PostClient.getPostList()
  28. // Get post by id
  29. const postById = await PostClient.getPost({ path: { id: '1' } })
  30. // Create a post
  31. const createPost = await PostClient.createPost({
  32. data: {
  33. userId: 1,
  34. title: 'Post title',
  35. body: 'Post body'
  36. }
  37. })
  38. // Put data to the post
  39. const putPost = await PostClient.putPost({
  40. path: {
  41. id: '1'
  42. },
  43. data: {
  44. userId: 2,
  45. title: 'Modified post title',
  46. body: 'Modified post body'
  47. }
  48. })
  49. // Patch the post
  50. const patchPost = await PostClient.patchPost({
  51. path: {
  52. id: '1'
  53. },
  54. data: {
  55. body: 'Modified post body for patch'
  56. }
  57. })
  58. // Delete the post
  59. const deletePost = await PostClient.deletePost({
  60. path: {
  61. id: '1'
  62. }
  63. })
  64. })()

Micro Services (like Feign)

  1. // flow
  2. import { AxiosPromise } from 'axios'
  3. import { GET, Client, ClientConfigurator } from 'axios-decorators'
  4. // Assume that this map is generated using consul service info
  5. const serviceMap = {
  6. PostService: 'http://172.0.0.1',
  7. CommentService: 'http://172.0.0.2'
  8. }
  9. ClientConfigurator.setBaseURL((serviceName) => {
  10. // Service name is used to get service URL
  11. return serviceMap[serviceName]
  12. })
  13. // Client definitions
  14. @Client({ name: 'PostService', basePath: '/post' })
  15. class PostClient {
  16. @GET('/posts')
  17. static getPostList (): AxiosPromise<PostType> {}
  18. }
  19. @Client({ name: 'CommentService', basePath: '/comment' })
  20. class CommentClient {
  21. @GET('/comments')
  22. static getCommentList (): AxiosPromise<CommentType> {}
  23. }

Reference

ClientConfigurator

It stores client configurations.

ClientConfigurator.setBaseURL

  1. // flow
  2. ClientConfigurator.setBaseURL(baseURL: string | ((serviceName: string) => void))
Property Description Type
baseURL It is global Base URL. string ((serviceName: string) => void)

ClientConfigurator.setHeaders

  1. // flow
  2. ClientConfigurator.setBaseURL(headers: object | ((serviceName: string) => void))
Property Description Type
headers It is global headers value. object ((serviceName: string) => void)

Decorators

@Client

It is used to define client class.

  1. // flow
  2. Client(name?: string);
Property Description Type
options.name It is distinctive feature of the client class. It could be used to find service’s IP address. string
  1. // flow
  2. Client(options?: { name?: string; baseURL?: string; basePath?: string; });
Property Description Type
options.name It is distinctive feature of the client class. It could be used to find service’s IP address. string
options.baseURL Base URL of the API can be changed directly using this parameter. It overrides baseURL coming from ClientConfigurator. string
options.basePath It is common path of the service. string

Methods (@GET, @POST, @PUT, @PATCH, @DELETE)

  1. // flow
  2. type MethodOptions = {
  3. headers?: object | (() => object);
  4. };
  5. GET(endpoint: string, methodOptions?: MethodOptions);
  6. POST(endpoint: string, methodOptions?: MethodOptions);
  7. PUT(endpoint: string, methodOptions?: MethodOptions);
  8. PATCH(endpoint: string, methodOptions?: MethodOptions);
  9. DELETE(endpoint: string, methodOptions?: MethodOptions);
Property Description Type
endpoint This is specific path of your controller. string
methodOptions.headers Headers of the request. object function