WAPI
WAPI is a middleware for connect/express that make the common tasks in an API easier.
Setup
var express = ;var wapi = ;var port = processenvPORT || 3004;var app = ; var api = { //... } { //... } app; app
Options (optional)
Option | Detail | Default Value |
---|---|---|
prefix | Is the prefix path for all the API calls | "/api/v1" |
Endpoints
Every method of the API object is related to an URL. For example: getUsers()
will
receive all the GET
HTTP requests to the /api/v1/users
endpoint.
Every method has to return a Promise.
var api = { return dbusers; } { // ... } { // ... } { // ... } app;
req
Every API endpoint receive selected data from the request.
Config | Detail |
---|---|
resourceName | /api/v1/{resourceName} |
baseURL | base url of the endpoint |
id | /api/v1/{resourceName}/{id} |
body / payload | The body from a POST/PUT request for JSON and multipart |
query / options | Query string params |
files | The files sent from a multipart request |
access_token | Bearer 1234 authorization header or /api/v1/user/?access_token=1234 |
headers | HTTP headers |
IMPORTANT: In order to client connect with the server correctly, set BASE_URL
environment variable in the server. Example: http://api.myserver.com/
Otherwise, WAPI server will try to resolve this value but with certain risk of mistake.
Client Side - Angular
<!-- Your code -->
PST! If the app is already an Angular application just call ng-wapi.js
and
set the ngWapi
module as dependency of your app.
// example angular;
Tips
- Avoid
{{var}}
to print text, useng-bind
instead. This prevent the blink effect when variables doesn't exist yet.
Root Scope vars
This variables are available in all the ngWapi scope
Var | Type | Detail |
---|---|---|
wLocation | Object | Current URL representation |
Forms
In order to connect a form to an endpoint of the API
- Setup the
w-form
directive with the endpoint name as the value (eg:users
,contact
) - Place the
ng-submit
listener and call thesubmit()
method - Set all the input that you wanna send with
ng-model="data.fieldName"
- Use
ng-show
to showsending
,submitted
orfail
states - Use on-response-redirect attribute if want to redirect when submit finished. It is an expression so, it can access to the scope.
- Add
file
attribute to create attach files buttons (dropzone) and use thename
attribute.
- In order to customize the preview of the files overwrite this CSS classes:
.dz-preview
.dz-image
.dz-details
.dz-progress
.dz-error-message
.dz-success-mark
.dz-error-mark
Scope vars
Var | Type | Detail |
---|---|---|
data | Object | Data to be submitted |
submitting / sending | Boolean | form is posting |
submitted / success | Boolean | the request is complete and no errors present |
response | Object | the response of the request |
redirecting | Boolean | when is redirecting |
fail | Boolean | Query string params |
submit | function | Function to be called in order to init the form post |
Attributes
Attribute | Type | Detail |
---|---|---|
w-form | String | Start the directive and set the name of the endpoint to post |
on-response-redirect | Expression | URL to be redirected after post |
redirect-delay | Number | Milliseconds to wait before redirect |
Enviar CV Pic Enviando Great :( <!-- Also, we have the server response in a 'response' object --> {{response}}
Get a resource
Scope vars
Var | Type | Detail |
---|---|---|
data | Object | Data received from the endpoint |
Attributes | Type | Detail |
---|---|---|
w-get | String | Start the directive and set the path of the endpoint to be called |
Product name {{data.name}}
Client side - Vanilla
...
Forms
It use the .wapi-form-wrapper
.wapi-form
.wapi-form-done
.wapi-form-fail
classes to work
IMPORTANT: data-form-name attribute is necesary in the wrapper
<!-- HTML Structure - Look the classes !! and the data-form-name attribute --> Enviar Great :( ...