Skip to content

Commit

Permalink
refactored compute script to edge scripting scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
ToshY committed Nov 17, 2024
1 parent 093914f commit 7f8a2ac
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 163 deletions.
108 changes: 107 additions & 1 deletion docs/edge-scripting-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,113 @@ $edgeScriptingApi->getCode(
$edgeScriptingApi->setCode(
id: 1,
body: [
'Code' => "export default function handleQuery(event) {\n console.log(\"Hello world!\")\n return new TxtRecord(\"Hello world!\");\n}",
'Code' => "import * as BunnySDK from \"https://esm.sh/@bunny.net/[email protected]\";\n\n/**\n * Returns an HTTP response.\n * @param {Request} request - The Fetch API Request object.\n * @return {Response} The HTTP response or string.\n */\nBunnySDK.net.http.serve(async (request: Request): Response | Promise<Response> => {\n const url = new URL(request.url);\n return new Response(\"Hello from \" + url.pathname);\n});\n",
],
);
```

### Edge Script

#### [List Edge Scripts](https://docs.bunny.net/reference/listedgescriptsendpoint_listedgescriptsbyaccount)

```php
$edgeScriptingApi->listEdgeScripts(
query: [
'page' => 1,
'perPage' => 1000,
'search' => 'Test',
'includeLinkedPullzones' => false,
'integrationId' => 1234,
],
);
```

#### [Add Edge Script](https://docs.bunny.net/reference/createedgescriptendpoint_addscript)

```php
$edgeScriptingApi->addEdgeScript(
body: [
'Name' => 'Test Script Modified',
'Code' => "import * as BunnySDK from \"https://esm.sh/@bunny.net/[email protected]\";\n\n/**\n * Returns an HTTP response.\n * @param {Request} request - The Fetch API Request object.\n * @return {Response} The HTTP response or string.\n */\nBunnySDK.net.http.serve(async (request: Request): Response | Promise<Response> => {\n const url = new URL(request.url);\n return new Response(\"Hello from \" + url.pathname);\n});\n",
'ScriptType' => 1,
'CreateLinkedPullZone' => true,
'LinkedPullZoneName' => 'MyPullZone',
'Integration' => [
'IntegrationId' => 1234,
'RepositorySettings' => [
'Id' => 1234,
'Name' => 'Test Repo',
'Private' => true,
'TemplateUrl' => 'https://example.com',
],
'DeployConfiguration' => [
'Branch' => 1234,
'InstallCommand' => 'Test Repo',
'BuildCommand' => 'true',
'EntryFile' => 'test.js',
'CreateWorkflow' => false,
],
],
],
);
```

!!! note

- The key `ScriptType` has the following possible values:
- `0` = DNS (DNS scripts)
- `1` = Standalone (Standalone scripts are ideal for a wide range of applications, such as building RESTful APIs, delivering UI applications, and processing data at the edge.)
- `2` = Middleware (Middleware scripts common use cases include user authentication, error handling, logging, security enhancements, A/B testing, HTML manipulation, and more.)


#### [Get Edge Script](https://docs.bunny.net/reference/getedgescriptbyidendpoint_getedgescriptbyid)

```php
$edgeScriptingApi->getEdgeScript(
id: 1,
);
```

#### [Get Edge Script Statistics](https://docs.bunny.net/reference/edgescriptstatisticsendpoint_getedgescriptstatisticsendpoint)

```php
$edgeScriptingApi->getEdgeScriptStatistics(
id: 1,
query: [
'dateFrom' => 'm-d-Y',
'dateTo' => 'm-d-Y',
'loadLatest' => false,
'hourly' => false,
],
);
```

#### [Update Edge Script](https://docs.bunny.net/reference/updateedgescriptendpoint_update)

```php
$edgeScriptingApi->updateEdgeScript(
id: 1,
body: [
'Name' => 'Test Script',
'ScriptType' => 1,
],
);
```

!!! note

- The key `ScriptType` has the following possible values:
- `0` = DNS (DNS scripts)
- `1` = Standalone (Standalone scripts are ideal for a wide range of applications, such as building RESTful APIs, delivering UI applications, and processing data at the edge.)
- `2` = Middleware (Middleware scripts common use cases include user authentication, error handling, logging, security enhancements, A/B testing, HTML manipulation, and more.)

#### [Delete Edge Script](https://docs.bunny.net/reference/deleteedgescriptendpoint_delete)

```php
$edgeScriptingApi->deleteEdgeScript(
id: 1,
query: [
'deleteLinkedPullZones' => false,
],
);
```
Expand Down
107 changes: 0 additions & 107 deletions src/BaseAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@
use ToshY\BunnyNet\Model\API\Base\Billing\GetBillingSummaryPDF;
use ToshY\BunnyNet\Model\API\Base\Billing\GetCoinifyBitcoinExchangeRate;
use ToshY\BunnyNet\Model\API\Base\Billing\PreparePaymentAuthorization;
use ToshY\BunnyNet\Model\API\Base\Compute\AddComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\AddComputeScriptVariable;
use ToshY\BunnyNet\Model\API\Base\Compute\DeleteComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\DeleteComputeScriptVariable;
use ToshY\BunnyNet\Model\API\Base\Compute\GetComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\GetComputeScriptVariable;
use ToshY\BunnyNet\Model\API\Base\Compute\ListComputeScriptReleases;
use ToshY\BunnyNet\Model\API\Base\Compute\ListComputeScripts;
use ToshY\BunnyNet\Model\API\Base\Compute\PublishComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\PublishComputeScriptByPathParameter;
use ToshY\BunnyNet\Model\API\Base\Compute\UpdateComputeScript;
use ToshY\BunnyNet\Model\API\Base\Compute\UpdateComputeScriptVariable;
use ToshY\BunnyNet\Model\API\Base\Countries\ListCountries;
use ToshY\BunnyNet\Model\API\Base\DNSZone\AddDNSRecord;
Expand Down Expand Up @@ -481,108 +476,6 @@ public function applyPromoCode(array $query): BunnyClientResponseInterface
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\ParameterIsRequiredException
* @param array<string,mixed> $query
* @return BunnyClientResponseInterface
*/
public function listComputeScripts(array $query = []): BunnyClientResponseInterface
{
$endpoint = new ListComputeScripts();

ParameterValidator::validate($query, $endpoint->getQuery());

return $this->client->request(
endpoint: $endpoint,
query: $query,
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\ParameterIsRequiredException
* @return BunnyClientResponseInterface
* @param array<string,mixed> $body
*/
public function addComputeScript(array $body): BunnyClientResponseInterface
{
$endpoint = new AddComputeScript();

ParameterValidator::validate($body, $endpoint->getBody());

return $this->client->request(
endpoint: $endpoint,
body: BodyContentHelper::getBody($body),
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @return BunnyClientResponseInterface
* @param int $id
*/
public function getComputeScript(int $id): BunnyClientResponseInterface
{
$endpoint = new GetComputeScript();

return $this->client->request(
endpoint: $endpoint,
parameters: [$id],
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @throws Exception\InvalidTypeForKeyValueException
* @throws Exception\InvalidTypeForListValueException
* @throws Exception\ParameterIsRequiredException
* @return BunnyClientResponseInterface
* @param int $id
* @param array<string,mixed> $body
*/
public function updateComputeScript(int $id, array $body): BunnyClientResponseInterface
{
$endpoint = new UpdateComputeScript();

ParameterValidator::validate($body, $endpoint->getBody());

return $this->client->request(
endpoint: $endpoint,
parameters: [$id],
body: BodyContentHelper::getBody($body),
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
* @throws Exception\JSONException
* @return BunnyClientResponseInterface
* @param int $id
*/
public function deleteComputeScript(int $id): BunnyClientResponseInterface
{
$endpoint = new DeleteComputeScript();

return $this->client->request(
endpoint: $endpoint,
parameters: [$id],
);
}

/**
* @throws ClientExceptionInterface
* @throws Exception\BunnyClientResponseException
Expand Down
Loading

0 comments on commit 7f8a2ac

Please sign in to comment.