Swagger.ApiRequest
This is a utility object to allow developers to make API request manually without being coupled to a view component. This utility object exposes six methods:
Get
Post
Put
Patch
Delete
Base
Base
is the master request method and the other five methods are specialised aliases. You can use Base
and one of the named methods and achieve exactly the same result.
Importing
There are 3 different ways to import this component. I’ve only shown it for Get
but its the same for others.
Take your pick.
import { ApiRequest: { Get }} from '../components/Swagger'
// Use it like this
Get(...).then().catch()
import { ApiRequest } from '../components/Swagger'
// Use it like this
ApiRequest.Get(...)
.then()
.catch()
import Swagger from '../components/Swagger'
// Use it like this
Swagger.ApiRequest
.Get(...)
.then()
.catch()
Usage
Each of them has the same syntax but slightly different params. Just slightly:
ApiRequest.<METHOD_NAME>(apiPath, {
query: {...},
path: {...},
body: {...},
})
apiPath
: The URL for the API request(see examples below)query
, path
, body
: ObjectsGet
ApiRequest.Get('/chapters/27');
ApiRequest.Base('get/api/v1/chapters/{chapterId}', {
path: {
chapterId: 27,
},
});
Post
ApiRequest.Post('/account/login', {
body: {
email: ...,
password: ...
}
});
ApiRequest.Base('post/api/v1/account/login', {
body: {
email: ...,
password: ...
}
});
Put
ApiRequest.Put('/user/current', {
body: {
firstName: ...,
lastName: ...
}
});
ApiRequest.Base('put/api/v1/user/current', {
body: {
firstName: ...,
lastName: ...
}
});
Patch
ApiRequest.Patch('/user/current', {
body: {
password: ...,
newPassword: ...,
currentPassword: ...
}
});
ApiRequest.Base('patch/api/v1/user/current', {
body: {
password: ...,
newPassword: ...,
currentPassword: ...
}
});
Delete
ApiRequest.Delete('/users/16/sessions/current?summary=true');
ApiRequest.Base('delete/api/v1/users/{id}/sessions/current', {
path: {
id: 16,
},
query: {
summary: true,
},
});