@phpsandbox/sdk 0.0.53 → 0.0.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2848,15 +2848,15 @@ var ReconnectingWebSocket = (
2848
2848
  };
2849
2849
  ReconnectingWebSocket3.prototype._getNextDelay = function() {
2850
2850
  var _a = this._options, _b = _a.reconnectionDelayGrowFactor, reconnectionDelayGrowFactor = _b === void 0 ? DEFAULT.reconnectionDelayGrowFactor : _b, _c = _a.minReconnectionDelay, minReconnectionDelay = _c === void 0 ? DEFAULT.minReconnectionDelay : _c, _d = _a.maxReconnectionDelay, maxReconnectionDelay = _d === void 0 ? DEFAULT.maxReconnectionDelay : _d;
2851
- var delay = 0;
2851
+ var delay2 = 0;
2852
2852
  if (this._retryCount > 0) {
2853
- delay = minReconnectionDelay * Math.pow(reconnectionDelayGrowFactor, this._retryCount - 1);
2854
- if (delay > maxReconnectionDelay) {
2855
- delay = maxReconnectionDelay;
2853
+ delay2 = minReconnectionDelay * Math.pow(reconnectionDelayGrowFactor, this._retryCount - 1);
2854
+ if (delay2 > maxReconnectionDelay) {
2855
+ delay2 = maxReconnectionDelay;
2856
2856
  }
2857
2857
  }
2858
- this._debug("next delay", delay);
2859
- return delay;
2858
+ this._debug("next delay", delay2);
2859
+ return delay2;
2860
2860
  };
2861
2861
  ReconnectingWebSocket3.prototype._wait = function() {
2862
2862
  var _this = this;
@@ -4617,6 +4617,198 @@ function isBeaconSupported() {
4617
4617
  }
4618
4618
  }
4619
4619
 
4620
+ // src/deployments.ts
4621
+ var NotebookDeploymentApi = class {
4622
+ constructor(client) {
4623
+ this.client = client;
4624
+ }
4625
+ async create(id, input = {}) {
4626
+ const response = await this.client.post(`/notebook/${id}/deployments`, input);
4627
+ return new DeploymentInstance(response.data, this.client);
4628
+ }
4629
+ async list(id) {
4630
+ const response = await this.client.get(`/notebook/${id}/deployments`);
4631
+ return response.data;
4632
+ }
4633
+ async slugAvailability(id, slug) {
4634
+ const query = new URLSearchParams({ slug }).toString();
4635
+ const response = await this.client.get(
4636
+ `/notebook/${id}/deployments/slug-availability?${query}`
4637
+ );
4638
+ return response.data;
4639
+ }
4640
+ };
4641
+ var DeploymentApi = class {
4642
+ constructor(client) {
4643
+ this.client = client;
4644
+ }
4645
+ async get(id) {
4646
+ const response = await this.client.get(`/deployments/${id}`);
4647
+ return new DeploymentInstance(response.data, this.client);
4648
+ }
4649
+ async redeploy(id, input = {}) {
4650
+ const response = await this.client.post(`/deployments/${id}/redeploy`, input);
4651
+ return new DeploymentInstance(response.data, this.client);
4652
+ }
4653
+ async delete(id) {
4654
+ await this.client.delete(`/deployments/${id}`);
4655
+ }
4656
+ async setProtection(id, input) {
4657
+ const response = await this.client.put(`/deployments/${id}/protection`, input);
4658
+ return new DeploymentInstance(response.data, this.client);
4659
+ }
4660
+ async protection(id) {
4661
+ const response = await this.client.get(`/deployments/${id}/protection`);
4662
+ return response.data;
4663
+ }
4664
+ async createProtectionSession(id) {
4665
+ const response = await this.client.post(`/deployments/${id}/protection/session`);
4666
+ return response.data;
4667
+ }
4668
+ async disableProtection(id) {
4669
+ const response = await this.client.delete(`/deployments/${id}/protection`);
4670
+ return new DeploymentInstance(response.data, this.client);
4671
+ }
4672
+ async setFeedback(id, input) {
4673
+ const response = await this.client.put(`/deployments/${id}/feedback`, input);
4674
+ return new DeploymentInstance(response.data, this.client);
4675
+ }
4676
+ async listDomains(id) {
4677
+ const response = await this.client.get(
4678
+ `/deployments/${id}/domains`
4679
+ );
4680
+ return unwrapDeploymentDomainCollection(response.data);
4681
+ }
4682
+ async createDomain(id, input) {
4683
+ const response = await this.client.post(`/deployments/${id}/domains`, input);
4684
+ return response.data;
4685
+ }
4686
+ async refreshDomain(id, domainId) {
4687
+ const response = await this.client.post(`/deployments/${id}/domains/${domainId}/refresh`);
4688
+ return response.data;
4689
+ }
4690
+ async deleteDomain(id, domainId) {
4691
+ await this.client.delete(`/deployments/${id}/domains/${domainId}`);
4692
+ }
4693
+ async events(id) {
4694
+ return parseNdjsonStream(await this.client.stream(`/deployments/${id}/events/stream`));
4695
+ }
4696
+ async buildLogs(id, buildId) {
4697
+ return parseNdjsonStream(
4698
+ await this.client.stream(`/deployments/${id}/builds/${buildId}/logs/stream`)
4699
+ );
4700
+ }
4701
+ async logs(id) {
4702
+ return parseNdjsonStream(await this.client.stream(`/deployments/${id}/logs/stream`));
4703
+ }
4704
+ };
4705
+ var DeploymentInstance = class {
4706
+ constructor(data, client) {
4707
+ this.data = data;
4708
+ this.client = client;
4709
+ }
4710
+ refresh() {
4711
+ return this.client.deployments.get(this.data.id);
4712
+ }
4713
+ redeploy(input = {}) {
4714
+ return this.client.deployments.redeploy(this.data.id, input);
4715
+ }
4716
+ delete() {
4717
+ return this.client.deployments.delete(this.data.id);
4718
+ }
4719
+ async wait(options = {}) {
4720
+ const intervalMs = options.intervalMs ?? 1e3;
4721
+ const timeoutMs = options.timeoutMs ?? 3e5;
4722
+ const startedAt = Date.now();
4723
+ let current = this;
4724
+ while (!deploymentStatusIsTerminal(current.data.status)) {
4725
+ if (Date.now() - startedAt > timeoutMs) {
4726
+ throw new Error(`Timed out waiting for deployment ${this.data.id}`);
4727
+ }
4728
+ await delay(intervalMs);
4729
+ current = await current.refresh();
4730
+ }
4731
+ return current;
4732
+ }
4733
+ events() {
4734
+ return this.client.deployments.events(this.data.id);
4735
+ }
4736
+ buildLogs(buildId = this.data.latest_build?.id ?? "") {
4737
+ if (buildId === "") {
4738
+ return Promise.reject(new Error("Deployment has no latest build id"));
4739
+ }
4740
+ return this.client.deployments.buildLogs(this.data.id, buildId);
4741
+ }
4742
+ logs() {
4743
+ return this.client.deployments.logs(this.data.id);
4744
+ }
4745
+ setProtection(input) {
4746
+ return this.client.deployments.setProtection(this.data.id, input);
4747
+ }
4748
+ protection() {
4749
+ return this.client.deployments.protection(this.data.id);
4750
+ }
4751
+ createProtectionSession() {
4752
+ return this.client.deployments.createProtectionSession(this.data.id);
4753
+ }
4754
+ disableProtection() {
4755
+ return this.client.deployments.disableProtection(this.data.id);
4756
+ }
4757
+ setFeedback(input) {
4758
+ return this.client.deployments.setFeedback(this.data.id, input);
4759
+ }
4760
+ domains() {
4761
+ return this.client.deployments.listDomains(this.data.id);
4762
+ }
4763
+ addDomain(input) {
4764
+ return this.client.deployments.createDomain(this.data.id, input);
4765
+ }
4766
+ refreshDomain(domainId) {
4767
+ return this.client.deployments.refreshDomain(this.data.id, domainId);
4768
+ }
4769
+ deleteDomain(domainId) {
4770
+ return this.client.deployments.deleteDomain(this.data.id, domainId);
4771
+ }
4772
+ };
4773
+ function deploymentStatusIsTerminal(status) {
4774
+ return ["healthy", "failed", "stopped", "deleted", "delete_failed"].includes(status);
4775
+ }
4776
+ function delay(ms) {
4777
+ return new Promise((resolve) => setTimeout(resolve, ms));
4778
+ }
4779
+ function unwrapDeploymentDomainCollection(input) {
4780
+ if (Array.isArray(input)) {
4781
+ return input;
4782
+ }
4783
+ return Array.isArray(input.data) ? input.data : [];
4784
+ }
4785
+ function parseNdjsonStream(input) {
4786
+ const decoder = new TextDecoder();
4787
+ let buffer = "";
4788
+ return input.pipeThrough(
4789
+ new TransformStream({
4790
+ transform(chunk, controller) {
4791
+ buffer += decoder.decode(chunk, { stream: true });
4792
+ const lines = buffer.split("\n");
4793
+ buffer = lines.pop() ?? "";
4794
+ for (const line of lines) {
4795
+ const trimmed = line.trim();
4796
+ if (trimmed !== "") {
4797
+ controller.enqueue(JSON.parse(trimmed));
4798
+ }
4799
+ }
4800
+ },
4801
+ flush(controller) {
4802
+ buffer += decoder.decode();
4803
+ const trimmed = buffer.trim();
4804
+ if (trimmed !== "") {
4805
+ controller.enqueue(JSON.parse(trimmed));
4806
+ }
4807
+ }
4808
+ })
4809
+ );
4810
+ }
4811
+
4620
4812
  // src/index.ts
4621
4813
  function normalizeNotebookSecretInputs(input) {
4622
4814
  if (Array.isArray(input)) {
@@ -4637,18 +4829,42 @@ var NotebookInitError = class extends Error {
4637
4829
  };
4638
4830
  var ApiError = class extends Error {
4639
4831
  constructor(response, body) {
4640
- super(`PHPSandbox API Error: ${response.status} ${response.statusText} - ${body}`);
4832
+ const data = parseApiErrorBody(body);
4833
+ const apiMessage = apiErrorMessage(data, body);
4834
+ super(`PHPSandbox API Error: ${response.status} ${response.statusText}${apiMessage === "" ? "" : ` - ${apiMessage}`}`);
4641
4835
  this.response = response;
4642
4836
  this.body = body;
4643
4837
  this.status = response.status;
4838
+ this.data = data;
4839
+ this.apiMessage = apiMessage === "" ? void 0 : apiMessage;
4644
4840
  }
4645
4841
  };
4842
+ function parseApiErrorBody(body) {
4843
+ try {
4844
+ return JSON.parse(body);
4845
+ } catch {
4846
+ return null;
4847
+ }
4848
+ }
4849
+ function apiErrorMessage(data, fallback) {
4850
+ if (data !== null && typeof data === "object") {
4851
+ const record = data;
4852
+ if (typeof record.message === "string" && record.message.trim() !== "") {
4853
+ return record.message.trim();
4854
+ }
4855
+ if (record.error !== void 0) {
4856
+ return typeof record.error === "string" ? record.error.trim() : JSON.stringify(record.error);
4857
+ }
4858
+ }
4859
+ return fallback.trim();
4860
+ }
4646
4861
  var NotebookApi = class {
4647
4862
  constructor(client) {
4648
4863
  this.client = client;
4649
4864
  this.secrets = new NotebookSecretApi(client);
4650
4865
  this.preview = new NotebookPreviewApi(client);
4651
4866
  this.mail = new NotebookMailApi(client);
4867
+ this.deployments = new NotebookDeploymentApi(client);
4652
4868
  }
4653
4869
  async create(template, input = {}, init = true) {
4654
4870
  const response = await this.client.post("/notebook", { template, ...input });
@@ -4700,6 +4916,7 @@ function normalizeApiBaseUrl(url) {
4700
4916
  var Client = class {
4701
4917
  constructor(token, url = "https://api.phpsandbox.io/v1", options = {}) {
4702
4918
  this.fetch = globalThis.fetch;
4919
+ this.deployments = new DeploymentApi(this);
4703
4920
  this.notebook = new NotebookApi(this);
4704
4921
  this.options = Object.assign({ startClosed: true }, options);
4705
4922
  this.baseUrl = normalizeApiBaseUrl(url);
@@ -4724,6 +4941,21 @@ var Client = class {
4724
4941
  put(path, body) {
4725
4942
  return this.makeRequest("PUT", path, { body: body === void 0 ? void 0 : JSON.stringify(body) });
4726
4943
  }
4944
+ async stream(path) {
4945
+ const response = await this.fetch(
4946
+ new Request(new URL(path.replace(/^\//, ""), this.baseUrl), {
4947
+ method: "GET",
4948
+ headers: this.headers
4949
+ })
4950
+ );
4951
+ if (!response.ok) {
4952
+ throw new ApiError(response, await response.text());
4953
+ }
4954
+ if (!response.body) {
4955
+ throw new Error("PHPSandbox API Error: response body is not readable");
4956
+ }
4957
+ return response.body;
4958
+ }
4727
4959
  async makeRequest(method, path, init) {
4728
4960
  const response = await this.fetch(
4729
4961
  new Request(new URL(path.replace(/^\//, ""), this.baseUrl), {
@@ -4870,6 +5102,9 @@ var NotebookInstance = class {
4870
5102
  fork(input = {}) {
4871
5103
  return this.client.notebook.fork(this.data.id, input);
4872
5104
  }
5105
+ deploy(input = {}) {
5106
+ return this.client.notebook.deployments.create(this.data.id, input);
5107
+ }
4873
5108
  gitCredentials() {
4874
5109
  return this.client.notebook.gitCredentials(this.data.id);
4875
5110
  }
@@ -5102,6 +5337,8 @@ export {
5102
5337
  BeaconTimeoutError,
5103
5338
  Client,
5104
5339
  CommandError,
5340
+ DeploymentApi,
5341
+ DeploymentInstance,
5105
5342
  ErrorEvent,
5106
5343
  FileChangeFilter,
5107
5344
  FileChangeType,
@@ -5112,6 +5349,7 @@ export {
5112
5349
  LspConnection,
5113
5350
  Navigator,
5114
5351
  NotebookApi,
5352
+ NotebookDeploymentApi,
5115
5353
  NotebookInitError,
5116
5354
  NotebookInstance,
5117
5355
  NotebookMail,