@innoflash/laravel-utils v1.0.5
LaravelUtils
This library is basically mapping the most common Laravel API responses to an Angular project
Table of contents
Installion
npm install @innoflash/laravel-utilsUsage
PS. Note that you can always customise the response by extending the interface to modify params according to your suit.
All the responses passing extra data have keys named
databut you can extend and override the key if you are wrapping your data with another keyMODELS
JWToken
This is used to map API JWT token into the project
- Used on login
- Used on token refresh
JSON Response:
{ "access_token": "access_token", "expires_in": 7200, "type": "bearer" }JWToken
Property Type Availability Description access_tokenstring mandatory The JSON Web Token to be used as a header on accessing the other middleware protected routes. expires_innumber mandatory The number of seconds the token is alive typestring mandatory The token type.
Date
Used on manipulating date responses.
JSON Response.
{ "approx": "3 hours ago", "formatted": "Mon 01 Jun 2020", "exact": "2020-06-01T12:46:28.000000Z", "time": "12:46" }Date
Property Type Availability Description approxstring mandatory The time in human readable form. formattedstring mandatory The date time in a formatted style. (Just the date) exactstring mandatory The date time as extracted from the DB. timestring mandatory The date time in a formatted style. (Just the time)
Pagination
This handles paginated data.
Links
Maps the links from pagination.
JSON Response.
{ "first": "http://localhost:8000/api/users?page=1", "last": "http://localhost:8000/api/users?page=5", "prev": null, "next": "http://localhost:8000/api/users?page=2" }Links
Property Type Availability Description firststring mandatory Link to the first page of the pagination. laststring mandatory Link to the last page of the pagination. prevstring mandatory Link to the previous page of the pagination. nextstring mandatory Link to the next page of the pagination.
Meta
Maps the meta data passed from pagination.
JSON Response.
{ "current_page": 1, "from": 3, "last_page": 5, "path": "http://localhost:8000/api/users", "per_page": 20, "to": 20, "total": 99 }Meta
Property Type Availability Description current_pagenumber mandatory The current page in the paginated data. fromnumber mandatory The number from which pagination is counting paginated data. last_pagenumber mandatory The last possible page number from paginated data. pathnumber mandatory The url being appended some page data. tonumber mandatory The number to which pagination is counting paginated data. totalnumber mandatory The total number of items in the paginated data. API RESPONSES
Action Response
Used mainly for retrieving results and responses from save and delete ops to name a few.
JSON Response.
{ "success": true, "message": "User deleted successfully", "data": [... optional data if any] }ActionResponse
Property Type Availability Description successboolean mandatory To flag whether a transaction succeeded or failed messagestring mandatory The message being send from the server. dataT (generic) optional The extra data you can send with for the transaction.
Example.
saveProfile(data: object): Observable<ActionResponse>{
return this.http.post<ActionResponse>(url, data)
}Error Response
Used mainly to process and render errors sent back by the server.
JSON Response.
{ "exception": "Illuminate\\Validation\\ValidationException", "statusCode": 422, "message": "The password field is required." }ErrorResponse
Property Type Availability Description exceptionstring mandatory Helps the backend developer to figure the kind of error the API is giving statusCodenumber mandatory The status code emitted by the failed API call. messagestring mandatory The message emitted by the failed API call
Example usage is when you are creating an API error message handler.
login(credentials: object): Observable<ActionResponse>{
return this.http.post<ActionResponse>(url, credentials)
.pipe(catchError(error => {
const serverError: ErrorResponse = error.error
throwError(serverError)
}))
}The consumer of the error will work with the error in this format.
Auth Response
Used mainly when you are authenticating users especially login and you wanna save the user details on a successful login.
JSON Response.
{ "user": { "id": 1, "user_name": "admin", "first_name": "Wendell", "last_name": "Halvorson", "email": "stoltenberg.meghan@example.org", "age": 30, "salary": 777084 }, "token": { "access_token": "access token", "expires_in": 7200, "type": "bearer" } }AuthResponse
Property Type Availability Description userT (generic) mandatory The user object brought from a successful login. tokenJWToken mandatory The auth token from a success model.
Collection Response
Used when pulling collections of data
- Usually from
Model::get()methodJSON Response.
{
"data": [
{
"id": 40,
"user_name": "odell80",
"first_name": "Rhiannon",
"last_name": "Brakus",
},
{
"id": 41,
"user_name": "flash",
"first_name": "Ruben",
"last_name": "Sikes",
}
]
}CollectionResponse
| Property | Type | Availability | Description |
|---|---|---|---|
data | T[] (generic) | mandatory | The collection of objects of type T |
Paginated Response
Used for mapping paginated results from the API.
JSON Response.
{ "data": [ { "id": 40, "user_name": "odell80", "first_name": "Rhiannon", }, { "id": 61, "user_name": "ykessler", } ], "links": { "first": "http://localhost:8000/api/users?page=1", "last": "http://localhost:8000/api/users?page=5", "prev": null, "next": "http://localhost:8000/api/users?page=2" }, "meta": { "current_page": 1, "from": 1, "last_page": 5, "path": "http://localhost:8000/api/users", "per_page": 20, "to": 20, "total": 99 } }PaginatedResponse
Property Type Availability Description dataT[] (generic) mandatory The collection of objects of type T linksLinks mandatory The links of the paginated data. metaMeta mandatory The meta data of the paginated data.