1.0.5 • Published 5 years ago

@innoflash/laravel-utils v1.0.5

Weekly downloads
-
License
-
Repository
-
Last release
5 years ago

LaravelUtils

This library is basically mapping the most common Laravel API responses to an Angular project

Table of contents

Installion

npm install @innoflash/laravel-utils

Usage

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 data but you can extend and override the key if you are wrapping your data with another key

MODELS

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

PropertyTypeAvailabilityDescription
access_tokenstringmandatoryThe JSON Web Token to be used as a header on accessing the other middleware protected routes.
expires_innumbermandatoryThe number of seconds the token is alive
typestringmandatoryThe 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

PropertyTypeAvailabilityDescription
approxstringmandatoryThe time in human readable form.
formattedstringmandatoryThe date time in a formatted style. (Just the date)
exactstringmandatoryThe date time as extracted from the DB.
timestringmandatoryThe 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
PropertyTypeAvailabilityDescription
firststringmandatoryLink to the first page of the pagination.
laststringmandatoryLink to the last page of the pagination.
prevstringmandatoryLink to the previous page of the pagination.
nextstringmandatoryLink 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
PropertyTypeAvailabilityDescription
current_pagenumbermandatoryThe current page in the paginated data.
fromnumbermandatoryThe number from which pagination is counting paginated data.
last_pagenumbermandatoryThe last possible page number from paginated data.
pathnumbermandatoryThe url being appended some page data.
tonumbermandatoryThe number to which pagination is counting paginated data.
totalnumbermandatoryThe 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

PropertyTypeAvailabilityDescription
successbooleanmandatoryTo flag whether a transaction succeeded or failed
messagestringmandatoryThe message being send from the server.
dataT (generic)optionalThe 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

PropertyTypeAvailabilityDescription
exceptionstringmandatoryHelps the backend developer to figure the kind of error the API is giving
statusCodenumbermandatoryThe status code emitted by the failed API call.
messagestringmandatoryThe 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

PropertyTypeAvailabilityDescription
userT (generic)mandatoryThe user object brought from a successful login.
tokenJWTokenmandatoryThe auth token from a success model.

Collection Response

Used when pulling collections of data

  • Usually from Model::get() method

    JSON 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

PropertyTypeAvailabilityDescription
dataT[] (generic)mandatoryThe 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

PropertyTypeAvailabilityDescription
dataT[] (generic)mandatoryThe collection of objects of type T
linksLinksmandatoryThe links of the paginated data.
metaMetamandatoryThe meta data of the paginated data.
1.0.5

5 years ago

1.0.4

5 years ago

1.0.1

5 years ago

1.0.3

5 years ago

1.0.0

5 years ago