1
0
Fork 0
mirror of synced 2024-06-01 18:39:57 +12:00

Added response filter

This commit is contained in:
Eldad Fux 2021-07-31 22:07:05 +03:00
parent c27fb0f000
commit fde3c0b112
4 changed files with 63 additions and 8 deletions

View file

@ -9,6 +9,7 @@ return [
'sdk' => false,
'docs' => false,
'tests' => false,
'optional' => false,
],
'console' => [
'key' => 'console',
@ -17,6 +18,7 @@ return [
'sdk' => false,
'docs' => false,
'tests' => false,
'optional' => false,
],
'account' => [
'key' => 'account',
@ -27,6 +29,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'avatars' => [
'key' => 'avatars',
@ -37,6 +40,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'database' => [
'key' => 'database',
@ -47,6 +51,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'locale' => [
'key' => 'locale',
@ -57,6 +62,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'health' => [
'key' => 'health',
@ -67,6 +73,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'projects' => [
'key' => 'projects',
@ -76,6 +83,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => false,
],
'storage' => [
'key' => 'storage',
@ -86,6 +94,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'teams' => [
'key' => 'teams',
@ -96,6 +105,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'users' => [
'key' => 'users',
@ -106,6 +116,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'functions' => [
'key' => 'functions',
@ -116,6 +127,7 @@ return [
'sdk' => true,
'docs' => true,
'tests' => false,
'optional' => true,
],
'mock' => [
'key' => 'mock',
@ -126,6 +138,7 @@ return [
'sdk' => false,
'docs' => false,
'tests' => true,
'optional' => false,
],
'graphql' => [
'key' => 'graphql',
@ -135,6 +148,7 @@ return [
'controller' => 'api/graphql.php',
'sdk' => false,
'docs' => false,
'tests' => false,
'tests' => true,
'optional' => false,
],
];

View file

@ -304,6 +304,8 @@ class Response extends SwooleResponse
return $this->payload;
}
$document = $model->filter($document);
foreach ($model->getRules() as $key => $rule) {
if (!$document->isSet($key)) {
if (!is_null($rule['default'])) {

View file

@ -2,6 +2,8 @@
namespace Appwrite\Utopia\Response;
use Utopia\Database\Document;
abstract class Model
{
const TYPE_STRING = 'string';
@ -30,19 +32,29 @@ abstract class Model
*/
protected $rules = [];
/**
* Filter Document Structure
*
* @return string
*/
public function filter(Document $document): Document
{
return $document;
}
/**
* Get Name
*
* @return string
*/
abstract public function getName():string;
abstract public function getName(): string;
/**
* Get Collection
*
* @return string
*/
abstract public function getType():string;
abstract public function getType(): string;
/**
* Get Rules

View file

@ -6,6 +6,7 @@ use stdClass;
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Response\Model;
use Utopia\Config\Config;
use Utopia\Database\Document;
class Project extends Model
{
@ -173,14 +174,18 @@ class Project extends Model
;
}
foreach ($services as $index => $service) {
$name = $method['name'] ?? '';
$key = $method['key'] ?? '';
foreach ($services as $service) {
if(!$service['optional']) {
continue;
}
$name = $service['name'] ?? '';
$key = $service['key'] ?? '';
$this
->addRule($key, [
->addRule('serviceStatusFor'.ucfirst($key), [
'type' => self::TYPE_BOOLEAN,
'description' => $name.' auth method status',
'description' => $name.' service status',
'example' => true,
'default' => true,
])
@ -207,4 +212,26 @@ class Project extends Model
{
return Response::MODEL_PROJECT;
}
/**
* Get Collection
*
* @return string
*/
public function filter(Document $document): Document
{
$values = $document->getAttribute('services', []);
$services = Config::getParam('services', []);
foreach($services as $service) {
if(!$service['optional']) {
continue;
}
$key = $service['key'] ?? '';
$value = $values[$key] ?? true;
$document->setAttribute('serviceStatusFor'.$key, $value);
}
return $document;
}
}