appwrite/app/controllers/api/functions.php

451 lines
19 KiB
PHP
Raw Normal View History

2020-05-04 14:35:01 +00:00
<?php
2022-05-24 14:28:27 +00:00
use Appwrite\Event\Event;
2022-02-18 12:36:24 +00:00
use Appwrite\Extend\Exception;
use Appwrite\Platform\Tasks\ScheduleExecutions;
2025-02-03 09:32:01 +00:00
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
2024-03-06 17:34:21 +00:00
use Appwrite\Utopia\Response;
use Utopia\App;
use Utopia\Config\Config;
use Utopia\Database\Database;
use Utopia\Database\DateTime;
use Utopia\Database\Document;
use Utopia\Database\Exception\Duplicate as DuplicateException;
2023-06-11 10:29:04 +00:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
2024-03-06 17:34:21 +00:00
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
2021-07-25 14:47:18 +00:00
use Utopia\Database\Validator\UID;
2020-05-05 17:30:12 +00:00
use Utopia\Validator\ArrayList;
2024-03-06 17:34:21 +00:00
use Utopia\Validator\Boolean;
2020-05-04 14:35:01 +00:00
use Utopia\Validator\Range;
2024-03-06 17:34:21 +00:00
use Utopia\Validator\Text;
2020-05-05 17:30:12 +00:00
use Utopia\Validator\WhiteList;
2020-05-04 14:35:01 +00:00
include_once __DIR__ . '/../shared/api.php';
2024-06-27 15:17:14 +00:00
App::delete('/v1/functions/:functionId/executions/:executionId')
->groups(['api', 'functions'])
->desc('Delete execution')
->label('scope', 'execution.write')
2024-10-29 15:07:12 +00:00
->label('resourceType', RESOURCE_TYPE_FUNCTIONS)
2024-06-27 15:17:14 +00:00
->label('event', 'functions.[functionId].executions.[executionId].delete')
->label('audits.event', 'executions.delete')
->label('audits.resource', 'function/{request.functionId}')
2025-02-03 09:32:01 +00:00
->label('sdk', new Method(
namespace: 'functions',
name: 'deleteExecution',
description: '/docs/references/functions/delete-execution.md',
auth: [AuthType::KEY],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_NOCONTENT,
model: Response::MODEL_NONE,
)
],
contentType: ContentType::NONE
))
2024-06-27 15:17:14 +00:00
->param('functionId', '', new UID(), 'Function ID.')
->param('executionId', '', new UID(), 'Execution ID.')
->inject('response')
->inject('dbForProject')
2025-02-03 09:32:01 +00:00
->inject('dbForPlatform')
2024-06-27 15:17:14 +00:00
->inject('queueForEvents')
2025-02-03 09:32:01 +00:00
->action(function (string $functionId, string $executionId, Response $response, Database $dbForProject, Database $dbForPlatform, Event $queueForEvents) {
2024-06-27 15:17:14 +00:00
$function = $dbForProject->getDocument('functions', $functionId);
if ($function->isEmpty()) {
throw new Exception(Exception::FUNCTION_NOT_FOUND);
}
$execution = $dbForProject->getDocument('executions', $executionId);
if ($execution->isEmpty()) {
throw new Exception(Exception::EXECUTION_NOT_FOUND);
}
2024-12-02 13:18:50 +00:00
if ($execution->getAttribute('resourceType') !== 'functions' && $execution->getAttribute('resourceInternalId') !== $function->getInternalId()) {
2024-06-27 15:17:14 +00:00
throw new Exception(Exception::EXECUTION_NOT_FOUND);
}
$status = $execution->getAttribute('status');
2024-06-27 15:17:14 +00:00
if (!in_array($status, ['completed', 'failed', 'scheduled'])) {
2024-06-27 15:17:14 +00:00
throw new Exception(Exception::EXECUTION_IN_PROGRESS);
}
if (!$dbForProject->deleteDocument('executions', $execution->getId())) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove execution from DB');
}
if ($status === 'scheduled') {
2025-02-03 09:32:01 +00:00
$schedule = $dbForPlatform->findOne('schedules', [
Query::equal('resourceId', [$execution->getId()]),
Query::equal('resourceType', [ScheduleExecutions::getSupportedResource()]),
Query::equal('active', [true]),
]);
if (!$schedule->isEmpty()) {
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
->setAttribute('active', false);
2025-02-03 09:32:01 +00:00
Authorization::skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), $schedule));
}
}
2024-06-27 15:17:14 +00:00
$queueForEvents
->setParam('functionId', $function->getId())
2024-07-03 07:02:41 +00:00
->setParam('executionId', $execution->getId())
->setPayload($response->output($execution, Response::MODEL_EXECUTION));
2024-06-27 15:17:14 +00:00
$response->noContent();
});
2022-08-01 15:13:47 +00:00
// Variables
2022-08-09 15:29:24 +00:00
App::post('/v1/functions/:functionId/variables')
2023-10-02 14:02:48 +00:00
->desc('Create variable')
2022-08-01 15:13:47 +00:00
->groups(['api', 'functions'])
->label('scope', 'functions.write')
2024-10-29 15:07:12 +00:00
->label('resourceType', RESOURCE_TYPE_FUNCTIONS)
2022-09-05 08:00:08 +00:00
->label('audits.event', 'variable.create')
2022-09-04 08:13:44 +00:00
->label('audits.resource', 'function/{request.functionId}')
2025-02-03 11:38:27 +00:00
->label('sdk', new Method(
namespace: 'functions',
name: 'createVariable',
description: '/docs/references/functions/create-variable.md',
auth: [AuthType::KEY],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_CREATED,
model: Response::MODEL_VARIABLE,
)
]
))
2022-09-19 10:05:42 +00:00
->param('functionId', '', new UID(), 'Function unique ID.', false)
->param('key', null, new Text(Database::LENGTH_KEY), 'Variable key. Max length: ' . Database::LENGTH_KEY . ' chars.', false)
2023-08-12 19:08:44 +00:00
->param('value', null, new Text(8192, 0), 'Variable value. Max length: 8192 chars.', false)
2025-02-11 12:50:54 +00:00
->param('secret', true, new Boolean(), 'Secret variables can be updated or deleted, but only functions can read them during build and runtime.', true)
2022-08-01 15:13:47 +00:00
->inject('response')
->inject('dbForProject')
2025-02-03 09:32:01 +00:00
->inject('dbForPlatform')
->action(function (string $functionId, string $key, string $value, bool $secret, Response $response, Database $dbForProject, Database $dbForPlatform) {
2022-08-01 15:13:47 +00:00
$function = $dbForProject->getDocument('functions', $functionId);
if ($function->isEmpty()) {
throw new Exception(Exception::FUNCTION_NOT_FOUND);
2022-08-01 15:13:47 +00:00
}
2022-08-26 13:38:39 +00:00
$variableId = ID::unique();
2022-08-01 15:13:47 +00:00
$variable = new Document([
2022-08-26 13:38:39 +00:00
'$id' => $variableId,
2022-08-24 15:07:18 +00:00
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
2023-03-11 16:06:02 +00:00
'resourceInternalId' => $function->getInternalId(),
'resourceId' => $function->getId(),
'resourceType' => 'function',
2022-08-01 15:13:47 +00:00
'key' => $key,
2022-08-26 13:38:39 +00:00
'value' => $value,
2024-10-21 14:33:57 +00:00
'secret' => $secret,
2023-03-11 16:06:02 +00:00
'search' => implode(' ', [$variableId, $function->getId(), $key, 'function']),
2022-08-01 15:13:47 +00:00
]);
try {
$variable = $dbForProject->createDocument('variables', $variable);
} catch (DuplicateException $th) {
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
2022-08-01 15:13:47 +00:00
}
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
2022-08-01 15:13:47 +00:00
2023-07-28 07:56:07 +00:00
// Inform scheduler to pull the latest changes
2025-02-03 09:32:01 +00:00
$schedule = $dbForPlatform->getDocument('schedules', $function->getAttribute('scheduleId'));
2023-06-11 10:29:04 +00:00
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
->setAttribute('schedule', $function->getAttribute('schedule'))
->setAttribute('active', !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deployment')));
2025-02-03 09:32:01 +00:00
Authorization::skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), $schedule));
2023-06-11 10:29:04 +00:00
2022-09-07 11:11:10 +00:00
$response
->setStatusCode(Response::STATUS_CODE_CREATED)
->dynamic($variable, Response::MODEL_VARIABLE);
2022-08-01 15:13:47 +00:00
});
2022-08-09 15:29:24 +00:00
App::get('/v1/functions/:functionId/variables')
2023-10-02 14:02:48 +00:00
->desc('List variables')
2022-08-01 15:13:47 +00:00
->groups(['api', 'functions'])
->label('scope', 'functions.read')
2024-10-29 15:07:12 +00:00
->label('resourceType', RESOURCE_TYPE_FUNCTIONS)
2025-02-03 11:38:27 +00:00
->label(
'sdk',
new Method(
namespace: 'functions',
name: 'listVariables',
description: '/docs/references/functions/list-variables.md',
auth: [AuthType::KEY],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_VARIABLE_LIST,
)
],
)
)
2022-09-19 10:05:42 +00:00
->param('functionId', '', new UID(), 'Function unique ID.', false)
2022-08-01 15:13:47 +00:00
->inject('response')
->inject('dbForProject')
->action(function (string $functionId, Response $response, Database $dbForProject) {
2022-08-01 15:13:47 +00:00
$function = $dbForProject->getDocument('functions', $functionId);
if ($function->isEmpty()) {
throw new Exception(Exception::FUNCTION_NOT_FOUND);
2022-08-01 15:13:47 +00:00
}
$response->dynamic(new Document([
2023-09-05 08:21:36 +00:00
'variables' => $function->getAttribute('vars', []),
'total' => \count($function->getAttribute('vars', [])),
2022-08-01 15:13:47 +00:00
]), Response::MODEL_VARIABLE_LIST);
});
2022-08-09 15:29:24 +00:00
App::get('/v1/functions/:functionId/variables/:variableId')
2023-10-02 14:02:48 +00:00
->desc('Get variable')
2022-08-01 15:13:47 +00:00
->groups(['api', 'functions'])
->label('scope', 'functions.read')
2024-10-29 15:07:12 +00:00
->label('resourceType', RESOURCE_TYPE_FUNCTIONS)
2022-08-03 13:32:50 +00:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2022-08-01 15:13:47 +00:00
->label('sdk.namespace', 'functions')
->label('sdk.method', 'getVariable')
->label('sdk.description', '/docs/references/functions/get-variable.md')
2022-08-01 15:13:47 +00:00
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_VARIABLE)
2022-09-19 10:05:42 +00:00
->param('functionId', '', new UID(), 'Function unique ID.', false)
->param('variableId', '', new UID(), 'Variable unique ID.', false)
2022-08-01 15:13:47 +00:00
->inject('response')
->inject('dbForProject')
2022-08-02 10:05:58 +00:00
->action(function (string $functionId, string $variableId, Response $response, Database $dbForProject) {
2022-08-01 15:13:47 +00:00
$function = $dbForProject->getDocument('functions', $functionId);
if ($function->isEmpty()) {
throw new Exception(Exception::FUNCTION_NOT_FOUND);
2022-08-01 15:13:47 +00:00
}
2023-07-24 13:12:36 +00:00
$variable = $dbForProject->getDocument('variables', $variableId);
2023-08-18 06:55:44 +00:00
if (
$variable === false ||
$variable->isEmpty() ||
$variable->getAttribute('resourceInternalId') !== $function->getInternalId() ||
$variable->getAttribute('resourceType') !== 'function'
) {
2023-07-24 13:12:36 +00:00
throw new Exception(Exception::VARIABLE_NOT_FOUND);
}
2022-08-01 15:13:47 +00:00
if ($variable === false || $variable->isEmpty()) {
throw new Exception(Exception::VARIABLE_NOT_FOUND);
2022-08-01 15:13:47 +00:00
}
$response->dynamic($variable, Response::MODEL_VARIABLE);
});
2022-08-09 15:29:24 +00:00
App::put('/v1/functions/:functionId/variables/:variableId')
2023-10-02 14:02:48 +00:00
->desc('Update variable')
2022-08-01 15:13:47 +00:00
->groups(['api', 'functions'])
->label('scope', 'functions.write')
2024-10-29 15:07:12 +00:00
->label('resourceType', RESOURCE_TYPE_FUNCTIONS)
2022-09-05 08:00:08 +00:00
->label('audits.event', 'variable.update')
2022-09-04 08:13:44 +00:00
->label('audits.resource', 'function/{request.functionId}')
2022-08-03 13:32:50 +00:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2022-08-01 15:13:47 +00:00
->label('sdk.namespace', 'functions')
->label('sdk.method', 'updateVariable')
->label('sdk.description', '/docs/references/functions/update-variable.md')
2022-08-01 15:13:47 +00:00
->label('sdk.response.code', Response::STATUS_CODE_OK)
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
->label('sdk.response.model', Response::MODEL_VARIABLE)
2022-09-19 10:05:42 +00:00
->param('functionId', '', new UID(), 'Function unique ID.', false)
->param('variableId', '', new UID(), 'Variable unique ID.', false)
->param('key', null, new Text(255), 'Variable key. Max length: 255 chars.', false)
2023-08-12 19:08:44 +00:00
->param('value', null, new Text(8192, 0), 'Variable value. Max length: 8192 chars.', true)
2025-02-11 12:50:54 +00:00
->param('secret', null, new Boolean(), 'Secret variables can be updated or deleted, but only functions can read them during build and runtime.', true)
2022-08-01 15:13:47 +00:00
->inject('response')
->inject('dbForProject')
2025-02-03 09:32:01 +00:00
->inject('dbForPlatform')
->action(function (string $functionId, string $variableId, string $key, ?string $value, ?bool $secret, Response $response, Database $dbForProject, Database $dbForPlatform) {
2022-08-01 15:13:47 +00:00
$function = $dbForProject->getDocument('functions', $functionId);
if ($function->isEmpty()) {
throw new Exception(Exception::FUNCTION_NOT_FOUND);
2022-08-01 15:13:47 +00:00
}
2023-07-24 13:12:36 +00:00
$variable = $dbForProject->getDocument('variables', $variableId);
if ($variable === false || $variable->isEmpty() || $variable->getAttribute('resourceInternalId') !== $function->getInternalId() || $variable->getAttribute('resourceType') !== 'function') {
throw new Exception(Exception::VARIABLE_NOT_FOUND);
}
2022-08-01 15:13:47 +00:00
if ($variable === false || $variable->isEmpty()) {
throw new Exception(Exception::VARIABLE_NOT_FOUND);
2022-08-01 15:13:47 +00:00
}
if ($variable->getAttribute('secret') === true && $secret === false) {
throw new Exception(Exception::VARIABLE_CANNOT_UNSET_SECRET);
}
2022-08-01 15:13:47 +00:00
$variable
->setAttribute('key', $key)
2022-08-01 15:13:47 +00:00
->setAttribute('value', $value ?? $variable->getAttribute('value'))
->setAttribute('secret', $secret ?? $variable->getAttribute('secret'))
2023-03-28 13:21:42 +00:00
->setAttribute('search', implode(' ', [$variableId, $function->getId(), $key, 'function']));
2022-08-01 15:13:47 +00:00
try {
$dbForProject->updateDocument('variables', $variable->getId(), $variable);
} catch (DuplicateException $th) {
throw new Exception(Exception::VARIABLE_ALREADY_EXISTS);
2022-08-01 15:13:47 +00:00
}
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
2022-08-01 15:13:47 +00:00
2023-07-28 07:56:07 +00:00
// Inform scheduler to pull the latest changes
2025-02-03 09:32:01 +00:00
$schedule = $dbForPlatform->getDocument('schedules', $function->getAttribute('scheduleId'));
2023-06-11 10:29:04 +00:00
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
->setAttribute('schedule', $function->getAttribute('schedule'))
->setAttribute('active', !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deployment')));
2025-02-03 09:32:01 +00:00
Authorization::skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), $schedule));
2023-06-11 10:29:04 +00:00
2022-08-01 15:13:47 +00:00
$response->dynamic($variable, Response::MODEL_VARIABLE);
});
2022-08-09 15:29:24 +00:00
App::delete('/v1/functions/:functionId/variables/:variableId')
2023-10-02 14:02:48 +00:00
->desc('Delete variable')
2022-08-01 15:13:47 +00:00
->groups(['api', 'functions'])
->label('scope', 'functions.write')
2024-10-29 15:07:12 +00:00
->label('resourceType', RESOURCE_TYPE_FUNCTIONS)
2022-09-05 08:00:08 +00:00
->label('audits.event', 'variable.delete')
2022-09-04 08:13:44 +00:00
->label('audits.resource', 'function/{request.functionId}')
2022-08-03 13:32:50 +00:00
->label('sdk.auth', [APP_AUTH_TYPE_KEY])
2022-08-01 15:13:47 +00:00
->label('sdk.namespace', 'functions')
->label('sdk.method', 'deleteVariable')
->label('sdk.description', '/docs/references/functions/delete-variable.md')
2022-08-01 15:13:47 +00:00
->label('sdk.response.code', Response::STATUS_CODE_NOCONTENT)
->label('sdk.response.model', Response::MODEL_NONE)
2022-09-19 10:05:42 +00:00
->param('functionId', '', new UID(), 'Function unique ID.', false)
->param('variableId', '', new UID(), 'Variable unique ID.', false)
2022-08-01 15:13:47 +00:00
->inject('response')
->inject('dbForProject')
2025-02-03 09:32:01 +00:00
->inject('dbForPlatform')
->action(function (string $functionId, string $variableId, Response $response, Database $dbForProject, Database $dbForPlatform) {
2022-08-01 15:13:47 +00:00
$function = $dbForProject->getDocument('functions', $functionId);
if ($function->isEmpty()) {
throw new Exception(Exception::FUNCTION_NOT_FOUND);
2022-08-01 15:13:47 +00:00
}
2023-07-24 13:12:36 +00:00
$variable = $dbForProject->getDocument('variables', $variableId);
if ($variable === false || $variable->isEmpty() || $variable->getAttribute('resourceInternalId') !== $function->getInternalId() || $variable->getAttribute('resourceType') !== 'function') {
throw new Exception(Exception::VARIABLE_NOT_FOUND);
}
2022-08-01 15:13:47 +00:00
if ($variable === false || $variable->isEmpty()) {
throw new Exception(Exception::VARIABLE_NOT_FOUND);
2022-08-01 15:13:47 +00:00
}
$dbForProject->deleteDocument('variables', $variable->getId());
2023-06-11 10:29:04 +00:00
$dbForProject->updateDocument('functions', $function->getId(), $function->setAttribute('live', false));
2022-08-01 15:13:47 +00:00
2023-07-28 07:56:07 +00:00
// Inform scheduler to pull the latest changes
2025-02-03 09:32:01 +00:00
$schedule = $dbForPlatform->getDocument('schedules', $function->getAttribute('scheduleId'));
2023-06-11 10:29:04 +00:00
$schedule
->setAttribute('resourceUpdatedAt', DateTime::now())
->setAttribute('schedule', $function->getAttribute('schedule'))
->setAttribute('active', !empty($function->getAttribute('schedule')) && !empty($function->getAttribute('deployment')));
2025-02-03 09:32:01 +00:00
Authorization::skip(fn () => $dbForPlatform->updateDocument('schedules', $schedule->getId(), $schedule));
2023-06-11 10:29:04 +00:00
2022-08-01 15:13:47 +00:00
$response->noContent();
});
2024-07-19 12:09:18 +00:00
App::get('/v1/functions/templates')
2024-08-13 12:59:37 +00:00
->groups(['api'])
2024-07-29 20:55:18 +00:00
->desc('List function templates')
2024-07-29 12:25:58 +00:00
->label('scope', 'public')
2024-10-29 15:07:12 +00:00
->label('resourceType', RESOURCE_TYPE_FUNCTIONS)
2025-02-03 09:32:01 +00:00
->label('sdk', new Method(
namespace: 'functions',
name: 'listTemplates',
description: '/docs/references/functions/list-templates.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_TEMPLATE_FUNCTION_LIST,
)
]
))
2024-07-29 13:29:16 +00:00
->param('runtimes', [], new ArrayList(new WhiteList(array_keys(Config::getParam('runtimes')), true), APP_LIMIT_ARRAY_PARAMS_SIZE), 'List of runtimes allowed for filtering function templates. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' runtimes are allowed.', true)
2024-07-30 10:41:04 +00:00
->param('useCases', [], new ArrayList(new WhiteList(['dev-tools','starter','databases','ai','messaging','utilities']), APP_LIMIT_ARRAY_PARAMS_SIZE), 'List of use cases allowed for filtering function templates. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' use cases are allowed.', true)
2024-07-30 09:43:38 +00:00
->param('limit', 25, new Range(1, 5000), 'Limit the number of templates returned in the response. Default limit is 25, and maximum limit is 5000.', true)
->param('offset', 0, new Range(0, 5000), 'Offset the list of returned templates. Maximum offset is 5000.', true)
2024-07-19 12:09:18 +00:00
->inject('response')
2024-07-29 13:29:16 +00:00
->action(function (array $runtimes, array $usecases, int $limit, int $offset, Response $response) {
2024-07-29 12:25:58 +00:00
$templates = Config::getParam('function-templates', []);
2024-07-29 13:29:16 +00:00
if (!empty($runtimes)) {
$templates = \array_filter($templates, function ($template) use ($runtimes) {
return \count(\array_intersect($runtimes, \array_column($template['runtimes'], 'name'))) > 0;
});
}
2024-07-29 12:25:58 +00:00
if (!empty($usecases)) {
$templates = \array_filter($templates, function ($template) use ($usecases) {
2024-07-30 10:41:04 +00:00
return \count(\array_intersect($usecases, $template['useCases'])) > 0;
2024-07-29 12:25:58 +00:00
});
}
$responseTemplates = \array_slice($templates, $offset, $limit);
2024-07-19 12:09:18 +00:00
$response->dynamic(new Document([
2024-07-29 12:25:58 +00:00
'templates' => $responseTemplates,
'total' => \count($responseTemplates),
2024-07-26 12:17:03 +00:00
]), Response::MODEL_TEMPLATE_FUNCTION_LIST);
2024-07-19 12:09:18 +00:00
});
2024-07-30 11:46:45 +00:00
App::get('/v1/functions/templates/:templateId')
->desc('Get function template')
->label('scope', 'public')
2024-10-29 15:07:12 +00:00
->label('resourceType', RESOURCE_TYPE_FUNCTIONS)
2025-02-03 09:32:01 +00:00
->label('sdk', new Method(
namespace: 'functions',
name: 'getTemplate',
description: '/docs/references/functions/get-template.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_TEMPLATE_FUNCTION,
)
]
))
2024-07-30 11:46:45 +00:00
->param('templateId', '', new Text(128), 'Template ID.')
->inject('response')
->action(function (string $templateId, Response $response) {
$templates = Config::getParam('function-templates', []);
2024-11-04 08:49:20 +00:00
$filtered = \array_filter($templates, function ($template) use ($templateId) {
2024-07-30 11:46:45 +00:00
return $template['id'] === $templateId;
2024-11-04 08:49:20 +00:00
});
$template = array_shift($filtered);
2024-07-30 11:46:45 +00:00
if (empty($template)) {
throw new Exception(Exception::FUNCTION_TEMPLATE_NOT_FOUND);
}
$response->dynamic(new Document($template), Response::MODEL_TEMPLATE_FUNCTION);
});