1
1
mirror of https://github.com/namibia/awesome-cheatsheets.git synced 2024-12-22 10:08:54 +00:00

docs(adonis): request

This commit is contained in:
Julien Le Coupanec 2020-04-13 17:10:16 +02:00
parent 490b5c1f59
commit 8ec2450866

View File

@ -7,32 +7,32 @@
* AVAILABLE CLI COMMANDS * AVAILABLE CLI COMMANDS
********************************************************************************************/ ********************************************************************************************/
build // Compile typescript code to Javascript. Optionally watch for file changes build // Compile typescript code to Javascript. Optionally watch for file changes.
invoke // Invoke post install instructions on a given AdonisJs package invoke // Invoke post install instructions on a given AdonisJs package.
serve // Compile typescript code to Javascript and start the HTTP server serve // Compile typescript code to Javascript and start the HTTP server.
// Dump // Dump
dump:rcfile // Dump contents of .adonisrc.json file along with defaults dump:rcfile // Dump contents of .adonisrc.json file along with defaults.
// Generate // Generate
generate:key // Generate a new APP_KEY secret generate:key // Generate a new APP_KEY secret.
generate:manifest // Generate manifest file to execute ace commands generate:manifest // Generate manifest file to execute ace commands.
// List // List
list:routes // List application routes list:routes // List application routes.
// Make // Make
make:command // Make a new ace command make:command // Make a new ace command.
make:controller // Make a new HTTP controller make:controller // Make a new HTTP controller.
make:middleware // Make a new middleware make:middleware // Make a new middleware.
make:migration // Make a new migration make:migration // Make a new migration.
make:provider // Make a new IoC container provider make:provider // Make a new IoC container provider.
make:validator // Make a new validator make:validator // Make a new validator.
make:view // Make a new view template make:view // Make a new view template.
// Migrations // Migrations
@ -43,34 +43,73 @@ migration:refresh // Rollback all migrations to the 0 batch then re-run them f
migration:reset // Rollback all migrations to the 0 batch. migration:reset // Rollback all migrations to the 0 batch.
migration:status // Get the status of all the migrations. migration:status // Get the status of all the migrations.
/********************************************************************************************
* REQUEST
********************************************************************************************/
request.all() // Returns an object containing all request data (merges query params and request body data).
request.get() // Returns an object containing query params data.
request.post() // Returns an object containing request body data.
request.raw() // Returns raw body data as a string.
request.only(['username', 'age']) // Returns an object with only the specified keys.
request.collect(['username', 'age']) // Formats so its ready to save to the database.
request.except(['csrf_token', 'submit']) // Returns an object with everything except the specified keys (opposite of only).
request.input('drink', 'coffee') // Get the value of a given key (if it doesnt exist, return the default value).
request.headers() // Returns an object of all header data.
request.header('some-other-header', 'default') // The header value for a given key (optionally with default value).
request.cookies() // Returns an object of all cookie data.
request.cookie('cart_total', 0) // Returns the cookie value for a given key (optionally with default value).
request.plainCookies() // Returns an object of all raw cookie data.
request.plainCookie('cart_total', 0) // Returns the raw cookie value for a given key (optionally with default value).
request.accepts(['json', 'html']) // Reads the Accept header to help determine the response format.
request.language(['en', 'fr']) // Language can also be negotiated based upon the Accept-Language header.
request.url() // Returns the current request url.
request.originalUrl() // Returns the full current request url with query strings.
request.method() // Returns the HTTP request method.
request.intended() // Returns the intended request HTTP method.
request.ip() // Returns the most trusted ip address for the user.
request.ips() // Returns an array of ips from most to the least trusted (removes the default ip address, which can be accessed via the ip method).
request.subdomains() // Returns a list of request subdomains (removes www from the list).
request.ajax() // Checks for X-Requested-With header to determine if the request is ajax or not.
request.pjax() // This methods looks for the X-PJAX header to identify if a request is pjax or not.
request.hostname() // Returns the request hostname.
request.protocol() // Return the request protocol.
request.match(['posts/:id']) // Returns whether the passed set of expressions match the current request URL.
request.hasBody() // A boolean indicating if the request has a post body (mainly used by the BodyParser to determine whether or not to parse the body).
request.is(['json', 'html']) // The is method returns the best matching content type for the current request. The check is entirely based upon the content-type header.
/******************************************************************************************** /********************************************************************************************
* ROUTING * ROUTING
********************************************************************************************/ ********************************************************************************************/
Route.get(url, closure) // Register route for GET verb Route.get(url, closure) // Register route for GET verb.
Route.post(url, closure) // Register route for POST verb Route.post(url, closure) // Register route for POST verb.
Route.put(url, closure) // Register route for PUT verb Route.put(url, closure) // Register route for PUT verb.
Route.patch(url, closure) // Register route for PATCH verb Route.patch(url, closure) // Register route for PATCH verb.
Route.delete(url, closure) // Register route for DELETED verb Route.delete(url, closure) // Register route for DELETED verb.
Route.any(url, closure) // Register route for all HTTP verbs Route.any(url, closure) // Register route for all HTTP verbs.
Route.on('/').render('welcome') // Render a view directly Route.on('/').render('welcome') // Render a view directly.
Route.route('/', () => {}, ['GET', 'POST', 'PUT']) // Register route for multiple verbs Route.route('/', () => {}, ['GET', 'POST', 'PUT']) // Register route for multiple verbs.
Route.get('users', closure).as('users.index') // Assign a unique name to the route Route.get('users', closure).as('users.index') // Assign a unique name to the route.
Route.get('users', closure).formats(['json', 'html'], true) // Force client to define the route format Route.get('users', closure).formats(['json', 'html'], true) // Force client to define the route format.
Route.resource('users', 'UserController') // Define a resource route for CRUD operations Route.resource('users', 'UserController') // Define a resource route for CRUD operations.
Route.resource('users', 'UserController').apiOnly() // Remove create and edit routes Route.resource('users', 'UserController').apiOnly() // Remove create and edit routes.
Route.resource('users', 'UserController').only(['index']) // Keeps only the passed routes Route.resource('users', 'UserController').only(['index']) // Keeps only the passed routes.
Route.resource('users', 'UserController').except(['index']) //Keeps all routes except the passed routes. Route.resource('users', 'UserController').except(['index']) //Keeps all routes except the passed routes.
Route.group(() => {}) // Define a group of routes Route.group(() => {}) // Define a group of routes.
Route.group(() => {}).middleware(['auth']) // Attach a middleware Route.group(() => {}).middleware(['auth']) // Attach a middleware.
Route.group(() => {}).formats(['json']) // Define response formats Route.group(() => {}).formats(['json']) // Define response formats.
Route.group(() => {}).prefix('api/v1') // Define a prefix for a group of routes Route.group(() => {}).prefix('api/v1') // Define a prefix for a group of routes.
Route.group(() => {}).namespace('Admin') // Prefix the namespace of the bound controller Route.group(() => {}).namespace('Admin') // Prefix the namespace of the bound controller.
Route.group(() => {}).domain('blog.sthg.com') // Specify which domain goup routes belong to Route.group(() => {}).domain('blog.sthg.com') // Specify which domain goup routes belong to.
/******************************************************************************************** /********************************************************************************************
* VALIDATOR * VALIDATOR
@ -86,12 +125,12 @@ const rules = {
// Indivative methods // Indivative methods
indicative.validate(data, rules); // Validate data with defined rules indicative.validate(data, rules); // Validate data with defined rules.
indicative.validateAll(data, rules); // Same as validate but continues to validate all fields, whereas the validate method stops on first error indicative.validateAll(data, rules); // Same as validate but continues to validate all fields, whereas the validate method stops on first error.
indicative.is.email(emailAddress); // Raw validator indicative.is.email(emailAddress); // Raw validator.
indicative.extend("exists", existsFn); // Add your own rules indicative.extend("exists", existsFn); // Add your own rules.
indicative.sanitize(data, rules); // Returns a new object with sanitized data: indicative.sanitize(data, rules); // Returns a new object with sanitized data:.
indicative.sanitizor.normalizeEmail(emailAddress); // Raw sanitizor indicative.sanitizor.normalizeEmail(emailAddress); // Raw sanitizor.
// Validations // Validations