mirror of
https://github.com/appwrite/appwrite.git
synced 2026-05-26 13:51:13 +00:00
Get Initial Draft Working
This commit is contained in:
@@ -98,14 +98,17 @@ class Schema
|
||||
foreach ($routes as $route) {
|
||||
/** @var Route $route */
|
||||
|
||||
$namespace = $route->getLabel('sdk.namespace', '');
|
||||
$method = $route->getLabel('sdk.method', '');
|
||||
$name = $namespace . \ucfirst($method);
|
||||
/** @var \Appwrite\SDK\Method $sdk */
|
||||
$sdk = $route->getLabel('sdk', false);
|
||||
|
||||
if (empty($name)) {
|
||||
if (empty($sdk)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$namespace = $sdk->getNamespace();
|
||||
$method = $sdk->getMethodName();
|
||||
$name = $namespace . \ucfirst($method);
|
||||
|
||||
foreach (Mapper::route($utopia, $route, $complexity) as $field) {
|
||||
switch ($route->getMethod()) {
|
||||
case 'GET':
|
||||
|
||||
@@ -86,7 +86,14 @@ class Mapper
|
||||
}
|
||||
}
|
||||
|
||||
$names = $route->getLabel('sdk.response.model', 'none');
|
||||
/** @var \Appwrite\SDK\Method $sdk */
|
||||
$sdk = $route->getLabel('sdk', false);
|
||||
|
||||
if (!$sdk) {
|
||||
return;
|
||||
}
|
||||
|
||||
$names = $sdk->getResponseModel() ?? [];
|
||||
$models = \is_array($names)
|
||||
? \array_map(static fn ($m) => static::$models[$m], $names)
|
||||
: [static::$models[$names]];
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Appwrite\Platform\Tasks;
|
||||
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\Specification\Format\OpenAPI3;
|
||||
use Appwrite\Specification\Format\Swagger2;
|
||||
use Appwrite\Specification\Specification;
|
||||
@@ -182,25 +183,31 @@ class Specs extends Action
|
||||
|
||||
foreach ($appRoutes as $key => $method) {
|
||||
foreach ($method as $route) {
|
||||
$hide = $route->getLabel('sdk.hide', false);
|
||||
$sdk = $route->getLabel('sdk', false);
|
||||
|
||||
/** @var \Appwrite\SDK\Method $sdk */
|
||||
if (empty($sdk)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$hide = $sdk->isHidden();
|
||||
if ($hide === true || (\is_array($hide) && \in_array($platform, $hide))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var \Utopia\Route $route */
|
||||
$routeSecurity = $route->getLabel('sdk.auth', []);
|
||||
$routeSecurity = $sdk->getAuth();
|
||||
$sdkPlatforms = [];
|
||||
|
||||
foreach ($routeSecurity as $value) {
|
||||
switch ($value) {
|
||||
case APP_AUTH_TYPE_SESSION:
|
||||
case AuthType::SESSION:
|
||||
$sdkPlatforms[] = APP_PLATFORM_CLIENT;
|
||||
break;
|
||||
case APP_AUTH_TYPE_JWT:
|
||||
case APP_AUTH_TYPE_KEY:
|
||||
case AuthType::JWT:
|
||||
case AuthType::KEY:
|
||||
$sdkPlatforms[] = APP_PLATFORM_SERVER;
|
||||
break;
|
||||
case APP_AUTH_TYPE_ADMIN:
|
||||
case AuthType::ADMIN:
|
||||
$sdkPlatforms[] = APP_PLATFORM_CONSOLE;
|
||||
break;
|
||||
}
|
||||
@@ -215,15 +222,15 @@ class Specs extends Action
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($route->getLabel('sdk.mock', false) && !$mocks) {
|
||||
if ($route->getLabel('mock', false) && !$mocks) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!$route->getLabel('sdk.mock', false) && $mocks) {
|
||||
if (!$route->getLabel('mock', false) && $mocks) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (empty($route->getLabel('sdk.namespace', null))) {
|
||||
if (empty($sdk->getNamespace())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
<?php
|
||||
|
||||
namespace Appwrite\SDK;
|
||||
|
||||
use Appwrite\Utopia\Response;
|
||||
use Swoole\Http\Response as HttpResponse;
|
||||
|
||||
enum AuthType: string {
|
||||
case JWT = APP_AUTH_TYPE_JWT;
|
||||
case KEY = APP_AUTH_TYPE_KEY;
|
||||
case SESSION = APP_AUTH_TYPE_SESSION;
|
||||
case ADMIN = APP_AUTH_TYPE_ADMIN;
|
||||
}
|
||||
|
||||
enum MethodType: string {
|
||||
case WEBAUTH = 'webAuth';
|
||||
case LOCATION = 'location';
|
||||
case GRAPHQL = 'graphql';
|
||||
case UPLOAD = 'upload';
|
||||
}
|
||||
|
||||
enum ResponseType: string {
|
||||
case NONE = '';
|
||||
case JSON = 'application/json';
|
||||
case IMAGE = 'image/*';
|
||||
case IMAGE_PNG = 'image/png';
|
||||
case MULTIPART = 'multipart/form-data';
|
||||
case HTML = 'text/html';
|
||||
case TEXT = 'text/plain';
|
||||
case ANY = '*/*';
|
||||
}
|
||||
|
||||
class Method
|
||||
{
|
||||
static array $knownMethods = [];
|
||||
|
||||
/**
|
||||
* Initialise a new SDK method
|
||||
*
|
||||
* @param array<AuthType> $authTypes
|
||||
* @param string $namespace
|
||||
* @param MethodType $methodType
|
||||
* @param string $desc
|
||||
* @param int $responseCode
|
||||
* @param string $responseModel
|
||||
* @param string $offlineKey
|
||||
* @param string $offlineModel
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function __construct(
|
||||
protected string $namespace,
|
||||
protected string $name,
|
||||
protected string $description,
|
||||
protected array $auth,
|
||||
protected int $responseCode,
|
||||
protected string|array $responseModel,
|
||||
protected ResponseType $responseType = ResponseType::JSON,
|
||||
protected ?MethodType $methodType = null,
|
||||
protected ?string $offlineKey = null,
|
||||
protected ?string $offlineModel = null,
|
||||
protected ?string $offlineResponseKey = null,
|
||||
protected bool $deprecated = false,
|
||||
protected array|bool $hide = false,
|
||||
protected bool $packaging = false,
|
||||
protected string $requestType = 'application/json',
|
||||
protected array $parameters = [],
|
||||
)
|
||||
{
|
||||
$this->validateMethod($name, $namespace);
|
||||
$this->validateAuthTypes($auth);
|
||||
// Disabled for now, will be enabled later
|
||||
// $this->validateDesc($description);
|
||||
$this->validateResponseModel($responseModel);
|
||||
|
||||
// No content check
|
||||
if ($responseCode === 204) {
|
||||
if ($responseModel !== Response::MODEL_NONE) {
|
||||
throw new \Exception("Error with {$this->getDebugName()} method: Response code 204 must have response model 'none'");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getDebugName(): string
|
||||
{
|
||||
return $this->namespace . '.' . $this->name;
|
||||
}
|
||||
|
||||
private function validateMethod(string $name, string $namespace): void
|
||||
{
|
||||
if (\in_array($this->getDebugName(), self::$knownMethods)) {
|
||||
throw new \Exception('Method ' . $name . ' already exists in namespace ' . $namespace);
|
||||
}
|
||||
|
||||
self::$knownMethods[] = $this->getDebugName();
|
||||
}
|
||||
|
||||
private function validateAuthTypes(array $authTypes): void
|
||||
{
|
||||
foreach ($authTypes as $authType) {
|
||||
if (!($authType instanceof AuthType)) {
|
||||
throw new \Exception("Error with {$this->getDebugName()} method: Invalid auth type");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function validateDesc(string $desc): void
|
||||
{
|
||||
if (empty($desc)) {
|
||||
throw new \Exception("Error with {$this->getDebugName()} method: Description file not set");
|
||||
}
|
||||
|
||||
$descPath = \realpath(__DIR__ . '/../../../' . $desc);
|
||||
|
||||
if (!\file_exists($descPath)) {
|
||||
throw new \Exception("Error with {$this->getDebugName()} method: Description file not found at {$descPath}");
|
||||
}
|
||||
}
|
||||
|
||||
private function validateResponseModel(string|array $responseModel): void
|
||||
{
|
||||
$response = new Response(new HttpResponse());
|
||||
|
||||
if (\is_array($responseModel)) {
|
||||
foreach ($responseModel as $model) {
|
||||
try {
|
||||
$response->getModel($model);
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception("Error with {$this->getDebugName()} method: Invalid response model, make sure the model has been defined in Response.php");
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$response->getModel($responseModel);
|
||||
} catch (\Exception $e) {
|
||||
throw new \Exception("Error with {$this->getDebugName()} method: Invalid response model, make sure the model has been defined in Response.php");
|
||||
}
|
||||
}
|
||||
|
||||
public function getNamespace(): string
|
||||
{
|
||||
return $this->namespace;
|
||||
}
|
||||
|
||||
public function getMethodName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getDescription(): string
|
||||
{
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function getAuth(): array
|
||||
{
|
||||
return $this->auth;
|
||||
}
|
||||
|
||||
public function getResponseCode(): int
|
||||
{
|
||||
return $this->responseCode;
|
||||
}
|
||||
|
||||
public function getResponseModel(): string|array
|
||||
{
|
||||
return $this->responseModel;
|
||||
}
|
||||
|
||||
public function getResponseType(): ResponseType
|
||||
{
|
||||
return $this->responseType;
|
||||
}
|
||||
|
||||
public function getMethodType(): ?MethodType
|
||||
{
|
||||
return $this->methodType;
|
||||
}
|
||||
|
||||
public function getOfflineKey(): ?string
|
||||
{
|
||||
return $this->offlineKey;
|
||||
}
|
||||
|
||||
public function getOfflineModel(): ?string
|
||||
{
|
||||
return $this->offlineModel;
|
||||
}
|
||||
|
||||
public function getOfflineResponseKey(): ?string
|
||||
{
|
||||
return $this->offlineResponseKey;
|
||||
}
|
||||
|
||||
public function isDeprecated(): bool
|
||||
{
|
||||
return $this->deprecated;
|
||||
}
|
||||
|
||||
public function isHidden(): bool|array
|
||||
{
|
||||
return $this->hide ?? false;
|
||||
}
|
||||
|
||||
public function isPackaging(): bool
|
||||
{
|
||||
return $this->packaging;
|
||||
}
|
||||
|
||||
public function getRequestType(): string
|
||||
{
|
||||
return $this->requestType;
|
||||
}
|
||||
|
||||
public function getParameters(): array
|
||||
{
|
||||
return $this->parameters;
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Appwrite\Specification\Format;
|
||||
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\MethodType;
|
||||
use Appwrite\Specification\Format;
|
||||
use Appwrite\Template\Template;
|
||||
use Appwrite\Utopia\Response\Model;
|
||||
@@ -120,32 +122,42 @@ class OpenAPI3 extends Format
|
||||
foreach ($this->routes as $route) {
|
||||
$url = \str_replace('/v1', '', $route->getPath());
|
||||
$scope = $route->getLabel('scope', '');
|
||||
$consumes = [$route->getLabel('sdk.request.type', 'application/json')];
|
||||
|
||||
$method = $route->getLabel('sdk.method', \uniqid());
|
||||
$sdk = $route->getLabel('sdk', false);
|
||||
|
||||
if (empty($sdk)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @var \Appwrite\SDK\Method $sdk
|
||||
*/
|
||||
$consumes = [$sdk->getRequestType()];
|
||||
|
||||
$method = $sdk->getMethodName() ?? \uniqid();
|
||||
|
||||
if (!empty($method) && is_array($method)) {
|
||||
$method = array_keys($method)[0];
|
||||
}
|
||||
|
||||
$desc = (!empty($route->getLabel('sdk.description', ''))) ? \realpath(__DIR__ . '/../../../../' . $route->getLabel('sdk.description', '')) : null;
|
||||
$produces = $route->getLabel('sdk.response.type', null);
|
||||
$model = $route->getLabel('sdk.response.model', 'none');
|
||||
$routeSecurity = $route->getLabel('sdk.auth', []);
|
||||
$desc = (!empty($sdk->getDescription())) ? \realpath(__DIR__ . '/../../../../' . $sdk->getDescription()) : null;
|
||||
$produces = ($sdk->getResponseType())->value;
|
||||
$model = $sdk->getResponseModel() ?? 'none';
|
||||
$routeSecurity = $sdk->getAuth() ?? [];
|
||||
$sdkPlatforms = [];
|
||||
|
||||
foreach ($routeSecurity as $value) {
|
||||
switch ($value) {
|
||||
case APP_AUTH_TYPE_SESSION:
|
||||
case AuthType::SESSION:
|
||||
$sdkPlatforms[] = APP_PLATFORM_CLIENT;
|
||||
break;
|
||||
case APP_AUTH_TYPE_KEY:
|
||||
case AuthType::KEY:
|
||||
$sdkPlatforms[] = APP_PLATFORM_SERVER;
|
||||
break;
|
||||
case APP_AUTH_TYPE_JWT:
|
||||
case AuthType::JWT:
|
||||
$sdkPlatforms[] = APP_PLATFORM_SERVER;
|
||||
break;
|
||||
case APP_AUTH_TYPE_ADMIN:
|
||||
case AuthType::ADMIN:
|
||||
$sdkPlatforms[] = APP_PLATFORM_CONSOLE;
|
||||
break;
|
||||
}
|
||||
@@ -156,34 +168,36 @@ class OpenAPI3 extends Format
|
||||
$sdkPlatforms[] = APP_PLATFORM_CLIENT;
|
||||
}
|
||||
|
||||
$namespace = $sdk->getNamespace() ?? 'default';
|
||||
|
||||
$temp = [
|
||||
'summary' => $route->getDesc(),
|
||||
'operationId' => $route->getLabel('sdk.namespace', 'default') . ucfirst($method),
|
||||
'tags' => [$route->getLabel('sdk.namespace', 'default')],
|
||||
'operationId' => $namespace . ucfirst($method),
|
||||
'tags' => [$namespace],
|
||||
'description' => ($desc) ? \file_get_contents($desc) : '',
|
||||
'responses' => [],
|
||||
'x-appwrite' => [ // Appwrite related metadata
|
||||
'method' => $method,
|
||||
'weight' => $route->getOrder(),
|
||||
'cookies' => $route->getLabel('sdk.cookies', false),
|
||||
'type' => $route->getLabel('sdk.methodType', ''),
|
||||
'deprecated' => $route->getLabel('sdk.deprecated', false),
|
||||
'demo' => Template::fromCamelCaseToDash($route->getLabel('sdk.namespace', 'default')) . '/' . Template::fromCamelCaseToDash($method) . '.md',
|
||||
'edit' => 'https://github.com/appwrite/appwrite/edit/master' . $route->getLabel('sdk.description', ''),
|
||||
'type' => $sdk->getMethodType()->value ?? '',
|
||||
'deprecated' => $sdk->isDeprecated(),
|
||||
'demo' => Template::fromCamelCaseToDash($namespace) . '/' . Template::fromCamelCaseToDash($method) . '.md',
|
||||
'edit' => 'https://github.com/appwrite/appwrite/edit/master' . $sdk->getDescription() ?? '',
|
||||
'rate-limit' => $route->getLabel('abuse-limit', 0),
|
||||
'rate-time' => $route->getLabel('abuse-time', 3600),
|
||||
'rate-key' => $route->getLabel('abuse-key', 'url:{url},ip:{ip}'),
|
||||
'scope' => $route->getLabel('scope', ''),
|
||||
'platforms' => $sdkPlatforms,
|
||||
'packaging' => $route->getLabel('sdk.packaging', false),
|
||||
'offline-model' => $route->getLabel('sdk.offline.model', ''),
|
||||
'offline-key' => $route->getLabel('sdk.offline.key', ''),
|
||||
'offline-response-key' => $route->getLabel('sdk.offline.response.key', '$id'),
|
||||
'packaging' => $sdk->isPackaging(),
|
||||
'offline-model' => $sdk->getOfflineModel() ?? '',
|
||||
'offline-key' => $sdk->getOfflineKey() ?? '',
|
||||
'offline-response-key' => $sdk->getOfflineResponseKey() ?? '$id',
|
||||
],
|
||||
];
|
||||
|
||||
if (is_array($route->getLabel('sdk.method', ''))) {
|
||||
$temp['x-appwrite']['multiplex'] = $route->getLabel('sdk.method', '');
|
||||
if (is_array($sdk->getMethodName() ?? '')) {
|
||||
$temp['x-appwrite']['multiplex'] = $sdk->getMethodName();
|
||||
}
|
||||
|
||||
foreach ($this->models as $value) {
|
||||
@@ -198,7 +212,7 @@ class OpenAPI3 extends Format
|
||||
}
|
||||
|
||||
if (!(\is_array($model)) && $model->isNone()) {
|
||||
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')] = [
|
||||
$temp['responses'][(string)$sdk->getResponseCode() ?? '500'] = [
|
||||
'description' => in_array($produces, [
|
||||
'image/*',
|
||||
'image/jpeg',
|
||||
@@ -219,7 +233,7 @@ class OpenAPI3 extends Format
|
||||
$usedModels[] = $m->getType();
|
||||
}
|
||||
|
||||
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')] = [
|
||||
$temp['responses'][(string)$sdk->getResponseCode() ?? '500'] = [
|
||||
'description' => $modelDescription,
|
||||
'content' => [
|
||||
$produces => [
|
||||
@@ -232,7 +246,7 @@ class OpenAPI3 extends Format
|
||||
} else {
|
||||
// Response definition using one type
|
||||
$usedModels[] = $model->getType();
|
||||
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')] = [
|
||||
$temp['responses'][(string)$sdk->getResponseCode() ?? '500'] = [
|
||||
'description' => $model->getName(),
|
||||
'content' => [
|
||||
$produces => [
|
||||
@@ -245,17 +259,18 @@ class OpenAPI3 extends Format
|
||||
}
|
||||
}
|
||||
|
||||
if ($route->getLabel('sdk.response.code', 500) === 204) {
|
||||
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')]['description'] = 'No content';
|
||||
unset($temp['responses'][(string)$route->getLabel('sdk.response.code', '500')]['schema']);
|
||||
if (($sdk->getResponseCode() ?? 500) === 204) {
|
||||
$temp['responses'][(string)$sdk->getResponseCode() ?? '500']['description'] = 'No content';
|
||||
unset($temp['responses'][(string)$sdk->getResponseCode() ?? '500']['schema']);
|
||||
}
|
||||
|
||||
if ((!empty($scope))) { // && 'public' != $scope
|
||||
$securities = ['Project' => []];
|
||||
|
||||
foreach ($route->getLabel('sdk.auth', []) as $security) {
|
||||
if (array_key_exists($security, $this->keys)) {
|
||||
$securities[$security] = [];
|
||||
foreach ($sdk->getAuth() as $security) {
|
||||
/** @var \Appwrite\SDK\AuthType $security */
|
||||
if (array_key_exists($security->value, $this->keys)) {
|
||||
$securities[$security->value] = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -306,7 +321,7 @@ class OpenAPI3 extends Format
|
||||
$node['schema']['x-example'] = false;
|
||||
break;
|
||||
case 'Appwrite\Utopia\Database\Validator\CustomId':
|
||||
if ($route->getLabel('sdk.methodType', '') === 'upload') {
|
||||
if ($sdk->getMethodType() === MethodType::UPLOAD) {
|
||||
$node['schema']['x-upload-id'] = true;
|
||||
}
|
||||
$node['schema']['type'] = $validator->getType();
|
||||
@@ -430,7 +445,7 @@ class OpenAPI3 extends Format
|
||||
$allowed = true;
|
||||
foreach ($this->enumBlacklist as $blacklist) {
|
||||
if (
|
||||
$blacklist['namespace'] == $route->getLabel('sdk.namespace', '')
|
||||
$blacklist['namespace'] == $sdk->getNamespace()
|
||||
&& $blacklist['method'] == $method
|
||||
&& $blacklist['parameter'] == $name
|
||||
) {
|
||||
@@ -441,8 +456,8 @@ class OpenAPI3 extends Format
|
||||
|
||||
if ($allowed) {
|
||||
$node['schema']['enum'] = $validator->getList();
|
||||
$node['schema']['x-enum-name'] = $this->getEnumName($route->getLabel('sdk.namespace', ''), $method, $name);
|
||||
$node['schema']['x-enum-keys'] = $this->getEnumKeys($route->getLabel('sdk.namespace', ''), $method, $name);
|
||||
$node['schema']['x-enum-name'] = $this->getEnumName($sdk->getNamespace() ?? '', $method, $name);
|
||||
$node['schema']['x-enum-keys'] = $this->getEnumKeys($sdk->getNamespace() ?? '', $method, $name);
|
||||
}
|
||||
if ($validator->getType() === 'integer') {
|
||||
$node['format'] = 'int32';
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Appwrite\Specification\Format;
|
||||
|
||||
use Appwrite\SDK\AuthType;
|
||||
use Appwrite\SDK\MethodType;
|
||||
use Appwrite\Specification\Format;
|
||||
use Appwrite\Template\Template;
|
||||
use Appwrite\Utopia\Response\Model;
|
||||
@@ -118,32 +120,40 @@ class Swagger2 extends Format
|
||||
/** @var \Utopia\Route $route */
|
||||
$url = \str_replace('/v1', '', $route->getPath());
|
||||
$scope = $route->getLabel('scope', '');
|
||||
$consumes = [$route->getLabel('sdk.request.type', 'application/json')];
|
||||
|
||||
$method = $route->getLabel('sdk.method', \uniqid());
|
||||
/** @var \Appwrite\SDK\Method $sdk */
|
||||
$sdk = $route->getLabel('sdk', false);
|
||||
|
||||
if (empty($sdk)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$consumes = [$sdk->getRequestType()];
|
||||
|
||||
$method = $sdk->getMethodName() ?? \uniqid();
|
||||
|
||||
if (!empty($method) && is_array($method)) {
|
||||
$method = array_keys($method)[0];
|
||||
}
|
||||
|
||||
$desc = (!empty($route->getLabel('sdk.description', ''))) ? \realpath(__DIR__ . '/../../../../' . $route->getLabel('sdk.description', '')) : null;
|
||||
$produces = $route->getLabel('sdk.response.type', null);
|
||||
$model = $route->getLabel('sdk.response.model', 'none');
|
||||
$routeSecurity = $route->getLabel('sdk.auth', []);
|
||||
$desc = (!empty($sdk->getDescription())) ? \realpath(__DIR__ . '/../../../../' . $sdk->getDescription()) : null;
|
||||
$produces = ($sdk->getResponseType())->value;
|
||||
$model = $sdk->getResponseModel() ?? 'none';
|
||||
$routeSecurity = $sdk->getAuth() ?? [];
|
||||
$sdkPlatforms = [];
|
||||
|
||||
foreach ($routeSecurity as $value) {
|
||||
switch ($value) {
|
||||
case APP_AUTH_TYPE_SESSION:
|
||||
case AuthType::SESSION:
|
||||
$sdkPlatforms[] = APP_PLATFORM_CLIENT;
|
||||
break;
|
||||
case APP_AUTH_TYPE_KEY:
|
||||
case AuthType::KEY:
|
||||
$sdkPlatforms[] = APP_PLATFORM_SERVER;
|
||||
break;
|
||||
case APP_AUTH_TYPE_JWT:
|
||||
case AuthType::JWT:
|
||||
$sdkPlatforms[] = APP_PLATFORM_SERVER;
|
||||
break;
|
||||
case APP_AUTH_TYPE_ADMIN:
|
||||
case AuthType::ADMIN:
|
||||
$sdkPlatforms[] = APP_PLATFORM_CONSOLE;
|
||||
break;
|
||||
}
|
||||
@@ -154,31 +164,33 @@ class Swagger2 extends Format
|
||||
$sdkPlatforms[] = APP_PLATFORM_CLIENT;
|
||||
}
|
||||
|
||||
$namespace = $sdk->getNamespace() ?? 'default';
|
||||
|
||||
$temp = [
|
||||
'summary' => $route->getDesc(),
|
||||
'operationId' => $route->getLabel('sdk.namespace', 'default') . ucfirst($method),
|
||||
'operationId' => $namespace . ucfirst($method),
|
||||
'consumes' => [],
|
||||
'produces' => [],
|
||||
'tags' => [$route->getLabel('sdk.namespace', 'default')],
|
||||
'tags' => [$namespace],
|
||||
'description' => ($desc) ? \file_get_contents($desc) : '',
|
||||
'responses' => [],
|
||||
'x-appwrite' => [ // Appwrite related metadata
|
||||
'method' => $method,
|
||||
'weight' => $route->getOrder(),
|
||||
'cookies' => $route->getLabel('sdk.cookies', false),
|
||||
'type' => $route->getLabel('sdk.methodType', ''),
|
||||
'deprecated' => $route->getLabel('sdk.deprecated', false),
|
||||
'demo' => Template::fromCamelCaseToDash($route->getLabel('sdk.namespace', 'default')) . '/' . Template::fromCamelCaseToDash($method) . '.md',
|
||||
'edit' => 'https://github.com/appwrite/appwrite/edit/master' . $route->getLabel('sdk.description', ''),
|
||||
'type' => $sdk->getMethodType()->value ?? '',
|
||||
'deprecated' => $sdk->isDeprecated(),
|
||||
'demo' => Template::fromCamelCaseToDash($namespace) . '/' . Template::fromCamelCaseToDash($method) . '.md',
|
||||
'edit' => 'https://github.com/appwrite/appwrite/edit/master' . $sdk->getDescription() ?? '',
|
||||
'rate-limit' => $route->getLabel('abuse-limit', 0),
|
||||
'rate-time' => $route->getLabel('abuse-time', 3600),
|
||||
'rate-key' => $route->getLabel('abuse-key', 'url:{url},ip:{ip}'),
|
||||
'scope' => $route->getLabel('scope', ''),
|
||||
'platforms' => $sdkPlatforms,
|
||||
'packaging' => $route->getLabel('sdk.packaging', false),
|
||||
'offline-model' => $route->getLabel('sdk.offline.model', ''),
|
||||
'offline-key' => $route->getLabel('sdk.offline.key', ''),
|
||||
'offline-response-key' => $route->getLabel('sdk.offline.response.key', '$id'),
|
||||
'packaging' => $sdk->isPackaging(),
|
||||
'offline-model' => $sdk->getOfflineModel() ?? '',
|
||||
'offline-key' => $sdk->getOfflineKey() ?? '',
|
||||
'offline-response-key' => $sdk->getOfflineResponseKey() ?? '$id',
|
||||
],
|
||||
];
|
||||
|
||||
@@ -186,8 +198,8 @@ class Swagger2 extends Format
|
||||
$temp['produces'][] = $produces;
|
||||
}
|
||||
|
||||
if (is_array($route->getLabel('sdk.method', ''))) {
|
||||
$temp['x-appwrite']['multiplex'] = $route->getLabel('sdk.method', '');
|
||||
if (is_array($sdk->getMethodName() ?? '')) {
|
||||
$temp['x-appwrite']['multiplex'] = $sdk->getMethodName();
|
||||
}
|
||||
|
||||
foreach ($this->models as $value) {
|
||||
@@ -202,7 +214,7 @@ class Swagger2 extends Format
|
||||
}
|
||||
|
||||
if (!(\is_array($model)) && $model->isNone()) {
|
||||
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')] = [
|
||||
$temp['responses'][(string)$sdk->getResponseCode() ?? '500'] = [
|
||||
'description' => in_array($produces, [
|
||||
'image/*',
|
||||
'image/jpeg',
|
||||
@@ -224,7 +236,7 @@ class Swagger2 extends Format
|
||||
foreach ($model as $m) {
|
||||
$usedModels[] = $m->getType();
|
||||
}
|
||||
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')] = [
|
||||
$temp['responses'][(string)$sdk->getResponseCode() ?? '500'] = [
|
||||
'description' => $modelDescription,
|
||||
'schema' => [
|
||||
'x-oneOf' => \array_map(function ($m) {
|
||||
@@ -235,7 +247,7 @@ class Swagger2 extends Format
|
||||
} else {
|
||||
// Response definition using one type
|
||||
$usedModels[] = $model->getType();
|
||||
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')] = [
|
||||
$temp['responses'][(string)$sdk->getResponseCode() ?? '500'] = [
|
||||
'description' => $model->getName(),
|
||||
'schema' => [
|
||||
'$ref' => '#/definitions/' . $model->getType(),
|
||||
@@ -244,17 +256,18 @@ class Swagger2 extends Format
|
||||
}
|
||||
}
|
||||
|
||||
if (in_array($route->getLabel('sdk.response.code', 500), [204, 301, 302, 308], true)) {
|
||||
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')]['description'] = 'No content';
|
||||
unset($temp['responses'][(string)$route->getLabel('sdk.response.code', '500')]['schema']);
|
||||
if (in_array($sdk->getResponseCode() ?? 500, [204, 301, 302, 308], true)) {
|
||||
$temp['responses'][(string)$sdk->getResponseCode() ?? '500']['description'] = 'No content';
|
||||
unset($temp['responses'][(string)$sdk->getResponseCode() ?? '500']['schema']);
|
||||
}
|
||||
|
||||
if ((!empty($scope))) { // && 'public' != $scope
|
||||
$securities = ['Project' => []];
|
||||
|
||||
foreach ($route->getLabel('sdk.auth', []) as $security) {
|
||||
if (array_key_exists($security, $this->keys)) {
|
||||
$securities[$security] = [];
|
||||
foreach ($sdk->getAuth() as $security) {
|
||||
/** @var \Appwrite\SDK\AuthType $security */
|
||||
if (array_key_exists($security->value, $this->keys)) {
|
||||
$securities[$security->value] = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,7 +288,7 @@ class Swagger2 extends Format
|
||||
|
||||
$parameters = \array_merge(
|
||||
$route->getParams(),
|
||||
$route->getLabel('sdk.parameters', []),
|
||||
$sdk->getParameters() ?? [],
|
||||
);
|
||||
|
||||
foreach ($parameters as $name => $param) { // Set params
|
||||
@@ -325,7 +338,7 @@ class Swagger2 extends Format
|
||||
$node['x-example'] = false;
|
||||
break;
|
||||
case 'Appwrite\Utopia\Database\Validator\CustomId':
|
||||
if ($route->getLabel('sdk.methodType', '') === 'upload') {
|
||||
if ($sdk->getMethodType() === MethodType::UPLOAD) {
|
||||
$node['x-upload-id'] = true;
|
||||
}
|
||||
$node['type'] = $validator->getType();
|
||||
@@ -432,7 +445,7 @@ class Swagger2 extends Format
|
||||
//Iterate the blackList. If it matches with the current one, then it is blackListed
|
||||
$allowed = true;
|
||||
foreach ($this->enumBlacklist as $blacklist) {
|
||||
if ($blacklist['namespace'] == $route->getLabel('sdk.namespace', '') && $blacklist['method'] == $method && $blacklist['parameter'] == $name) {
|
||||
if ($blacklist['namespace'] == $namespace && $blacklist['method'] == $method && $blacklist['parameter'] == $name) {
|
||||
$allowed = false;
|
||||
break;
|
||||
}
|
||||
@@ -440,8 +453,8 @@ class Swagger2 extends Format
|
||||
|
||||
if ($allowed && $validator->getType() === 'string') {
|
||||
$node['enum'] = $validator->getList();
|
||||
$node['x-enum-name'] = $this->getEnumName($route->getLabel('sdk.namespace', ''), $method, $name);
|
||||
$node['x-enum-keys'] = $this->getEnumKeys($route->getLabel('sdk.namespace', ''), $method, $name);
|
||||
$node['x-enum-name'] = $this->getEnumName($namespace, $method, $name);
|
||||
$node['x-enum-keys'] = $this->getEnumKeys($namespace, $method, $name);
|
||||
}
|
||||
|
||||
if ($validator->getType() === 'integer') {
|
||||
|
||||
Reference in New Issue
Block a user