1
0
Fork 0
mirror of synced 2024-05-19 20:22:33 +12:00
appwrite/docs/tutorials/add-route.md

186 lines
6 KiB
Markdown
Raw Normal View History

# Adding Route 🛡
2022-09-09 01:13:00 +12:00
2022-11-17 17:37:24 +13:00
This document is part of the Appwrite contributors' guide. Before you continue reading this document make sure you have read the [Code of Conduct](https://github.com/appwrite/.github/blob/main/CODE_OF_CONDUCT.md) and the [Contributing Guide](https://github.com/appwrite/appwrite/blob/master/CONTRIBUTING.md).
2022-09-09 01:13:00 +12:00
### 1. Alias
2022-11-17 17:37:24 +13:00
Setting an alias allows the route to be also accessible from the alias URL.
2022-11-17 17:37:24 +13:00
The first parameter specifies the alias URL, the second parameter specifies default values for route parameters.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/storage/buckets/:bucketId/files')
->alias('/v1/storage/files', ['bucketId' => 'default'])
```
### 2. Description
2022-11-17 17:37:24 +13:00
Used as an abstract description of the route.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/storage/buckets/:bucketId/files')
2022-09-10 01:40:34 +12:00
->desc('Create File')
2022-09-09 01:13:00 +12:00
```
### 3. Groups
2022-11-17 17:37:24 +13:00
2022-09-09 01:18:56 +12:00
Groups array is used to group one or more routes with one or more hooks functionality.
2022-11-17 17:37:24 +13:00
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/storage/buckets/:bucketId/files')
2022-09-10 02:30:37 +12:00
->groups(['api'])
2022-09-09 01:13:00 +12:00
```
2022-11-17 17:37:24 +13:00
In the above example groups() is used to define the current route as part of the routes that shares a common init middleware hook.
2022-09-09 01:13:00 +12:00
```php
App::init()
2022-09-10 01:40:34 +12:00
->groups(['api'])
->action(
2022-09-09 01:13:00 +12:00
some code.....
);
```
### 4. The Labels Mechanism
2022-11-17 17:37:24 +13:00
2022-09-10 01:21:26 +12:00
Labels are very strait forward and easy to use and understand, but in the same time are very robust.
Labels are passed from the controllers route and used to pick up key-value pairs to be handled in a centralized place
2022-09-09 01:13:00 +12:00
along the road.
Labels can be used to pass a pattern in order to be replaced in the other end.
Appwrite uses different labels to achieve different things, for example:
#### Scope
2022-11-17 17:37:24 +13:00
- scope - Defines the route permissions scope.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/storage/buckets/:bucketId/files')
->label('scope', 'files.write')
```
#### Audit
2022-09-09 01:13:00 +12:00
2022-11-17 17:37:24 +13:00
- audits.event - Identify the log in human-readable text.
- audits.userId - Signals the extraction of $userId in places that it's not available natively.
- audits.resource - Signals the extraction part of the resource.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/account/create')
2022-09-10 01:40:34 +12:00
->label('audits.event', 'account.create')
->label('audits.resource', 'user/{response.$id}')
->label('audits.userId', '{response.$id}')
2022-09-09 01:13:00 +12:00
```
2022-09-10 01:40:34 +12:00
#### SDK
2022-11-17 17:37:24 +13:00
- sdk.auth - Array of authentication types is passed in order to impose different authentication methods in different situations.
- sdk.namespace - Refers to the route namespace.
- sdk.method - Refers to the sdk method that needs to called.
- sdk.description - Description of the route,using markdown format.
- sdk.sdk.response.code - Refers to the route http response status code expected.
- sdk.auth.response.model - Refers the route http response expected.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/account/jwt')
2022-09-10 01:40:34 +12:00
->label('sdk.auth', [APP_AUTH_TYPE_SESSION])
->label('sdk.namespace', 'account')
->label('sdk.method', 'createJWT')
->label('sdk.description', '/docs/references/account/create-jwt.md')
->label('sdk.response.code', Response::STATUS_CODE_CREATED)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_JWT)
2022-09-09 01:13:00 +12:00
```
2022-09-10 01:21:26 +12:00
#### Cache
2022-11-17 17:37:24 +13:00
- cache - When set to true, signal the use of file cache.
- cache.resource - Identifies the cached resource.
2022-09-09 01:13:00 +12:00
```php
App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview')
2022-09-10 01:40:34 +12:00
->label('cache', true)
->label('cache.resource', 'file/{request.fileId}')
2022-09-09 01:13:00 +12:00
```
2022-09-10 01:21:26 +12:00
#### Abuse
2022-11-17 17:37:24 +13:00
- abuse-key - Specifies routes unique abuse key.
- abuse-limit - Specifies the number of times the route can be requested in a time frame, per route.
- abuse-time - Specifies the time frame (in seconds) relevancy of the all other abuse definitions, per route.
2022-09-10 02:00:08 +12:00
When using the example below, we configure the abuse mechanism to allow this key combination
2022-11-17 17:37:24 +13:00
constructed from the combination of the ip, http method, url, userId to hit the route maximum 60 times in 1 hour (60 seconds \* 60 minutes).
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/storage/buckets/:bucketId/files')
->label('abuse-key', 'ip:{ip},method:{method},url:{url},userId:{userId}')
2022-09-10 02:00:08 +12:00
->label('abuse-limit', 60)
2022-11-17 17:37:24 +13:00
->label('abuse-time', 3600)
2022-09-09 01:13:00 +12:00
```
2022-09-10 01:21:26 +12:00
#### Events
2022-11-17 17:37:24 +13:00
- event - A pattern that is associated with the route in behalf of realtime messaging.
Placeholders marked as `[]` are parsed and replaced with their real values.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/storage/buckets/:bucketId/files')
->label('event', 'buckets.[bucketId].files.[fileId].create')
```
2022-09-10 01:21:26 +12:00
#### Usage
2022-11-17 17:37:24 +13:00
- usage.metric - The metric the route generates.
- usage.params - Additional parameters the metrics can have.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/storage/buckets/:bucketId/files')
->label('usage.metric', 'files.{scope}.requests.create')
->label('usage.params', ['bucketId:{request.bucketId}'])
```
### 5. Param
2022-11-17 17:37:24 +13:00
As the name implies, `param()` is used to define a request parameter.
2022-09-09 01:13:00 +12:00
2022-09-10 02:29:11 +12:00
`param()` accepts 6 parameters :
2022-11-17 17:37:24 +13:00
- A key (name)
- A default value
- An instance of a validator class,This can also accept a callback that returns a validator instance. Dependency injection is supported for the callback.
- Description of the parameter
- Is the route optional
- An array of injections
2022-09-10 02:29:11 +12:00
2022-09-09 01:13:00 +12:00
```php
App::get('/v1/account/logs')
2022-09-10 01:40:34 +12:00
->param('queries', [], new Queries(new Limit(), new Offset()), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Only supported methods are limit and offset', true)
2022-09-09 01:13:00 +12:00
```
### 6. inject
2022-11-17 17:37:24 +13:00
inject is used to inject dependencies pre-bounded to the app.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/storage/buckets/:bucketId/files')
2022-09-10 01:40:34 +12:00
->inject('user')
2022-09-09 01:13:00 +12:00
```
In the example above, the user object is injected into the route pre-bounded using `App::setResource()`.
2022-09-09 01:13:00 +12:00
```php
App::setResource('user', function() {
some code...
});
```
### 6. Action
2022-11-17 17:37:24 +13:00
Action populates the actual routes code and has to be very clear and understandable. A good route stay simple and doesn't contain complex logic. An action is where we describe our business need in code, and combine different libraries to work together and tell our story.
2022-09-09 01:13:00 +12:00
```php
App::post('/v1/account/sessions/anonymous')
->action(function (Request $request) {
some code...
});
```