Files
dub-node/src/sdk/workspaces.ts
2024-05-01 11:03:53 +05:30

686 lines
23 KiB
TypeScript

/*
* Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT.
*/
import { SDKHooks } from "../hooks";
import { SDK_METADATA, SDKOptions, serverURLFromOptions } from "../lib/config";
import * as enc$ from "../lib/encodings";
import { HTTPClient } from "../lib/http";
import * as schemas$ from "../lib/schemas";
import { ClientSDK, RequestOptions } from "../lib/sdks";
import * as components from "../models/components";
import * as errors from "../models/errors";
import * as operations from "../models/operations";
import * as z from "zod";
export class Workspaces extends ClientSDK {
private readonly options$: SDKOptions & { hooks?: SDKHooks };
constructor(options: SDKOptions = {}) {
const opt = options as unknown;
let hooks: SDKHooks;
if (
typeof opt === "object" &&
opt != null &&
"hooks" in opt &&
opt.hooks instanceof SDKHooks
) {
hooks = opt.hooks;
} else {
hooks = new SDKHooks();
}
super({
client: options.httpClient || new HTTPClient(),
baseURL: serverURLFromOptions(options),
hooks,
});
this.options$ = { ...options, hooks };
void this.options$;
}
/**
* Retrieve a list of workspaces
*
* @remarks
* Retrieve a list of workspaces for the authenticated user.
*/
async list(options?: RequestOptions): Promise<Array<components.WorkspaceSchema>> {
const headers$ = new Headers();
headers$.set("user-agent", SDK_METADATA.userAgent);
headers$.set("Accept", "application/json");
const path$ = this.templateURLComponent("/workspaces")();
const query$ = "";
let security$;
if (typeof this.options$.token === "function") {
security$ = { token: await this.options$.token() };
} else if (this.options$.token) {
security$ = { token: this.options$.token };
} else {
security$ = {};
}
const context = {
operationID: "getWorkspaces",
oAuth2Scopes: [],
securitySource: this.options$.token,
};
const securitySettings$ = this.resolveGlobalSecurity(security$);
const doOptions = {
context,
errorCodes: [
"400",
"401",
"403",
"404",
"409",
"410",
"422",
"429",
"4XX",
"500",
"5XX",
],
};
const request = this.createRequest$(
context,
{
security: securitySettings$,
method: "GET",
path: path$,
headers: headers$,
query: query$,
},
options
);
const response = await this.do$(request, doOptions);
const responseFields$ = {
HttpMeta: {
Response: response,
Request: request,
},
};
if (this.matchResponse(response, 200, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return z.array(components.WorkspaceSchema$.inboundSchema).parse(val$);
},
"Response validation failed"
);
return result;
} else if (this.matchResponse(response, 400, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.BadRequest$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 401, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Unauthorized$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 403, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Forbidden$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 404, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.NotFound$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 409, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Conflict$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 410, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.InviteExpired$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 422, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.UnprocessableEntity$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 429, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.RateLimitExceeded$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 500, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.InternalServerError$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else {
const responseBody = await response.text();
throw new errors.SDKError(
"Unexpected API response status or content-type",
response,
responseBody
);
}
}
/**
* Create a workspace
*
* @remarks
* Create a new workspace for the authenticated user.
*/
async create(
input: operations.CreateWorkspaceRequestBody | undefined,
options?: RequestOptions
): Promise<components.WorkspaceSchema> {
const headers$ = new Headers();
headers$.set("user-agent", SDK_METADATA.userAgent);
headers$.set("Content-Type", "application/json");
headers$.set("Accept", "application/json");
const payload$ = schemas$.parse(
input,
(value$) =>
operations.CreateWorkspaceRequestBody$.outboundSchema.optional().parse(value$),
"Input validation failed"
);
const body$ =
payload$ === undefined ? null : enc$.encodeJSON("body", payload$, { explode: true });
const path$ = this.templateURLComponent("/workspaces")();
const query$ = "";
let security$;
if (typeof this.options$.token === "function") {
security$ = { token: await this.options$.token() };
} else if (this.options$.token) {
security$ = { token: this.options$.token };
} else {
security$ = {};
}
const context = {
operationID: "createWorkspace",
oAuth2Scopes: [],
securitySource: this.options$.token,
};
const securitySettings$ = this.resolveGlobalSecurity(security$);
const doOptions = {
context,
errorCodes: [
"400",
"401",
"403",
"404",
"409",
"410",
"422",
"429",
"4XX",
"500",
"5XX",
],
};
const request = this.createRequest$(
context,
{
security: securitySettings$,
method: "POST",
path: path$,
headers: headers$,
query: query$,
body: body$,
},
options
);
const response = await this.do$(request, doOptions);
const responseFields$ = {
HttpMeta: {
Response: response,
Request: request,
},
};
if (this.matchResponse(response, 200, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return components.WorkspaceSchema$.inboundSchema.parse(val$);
},
"Response validation failed"
);
return result;
} else if (this.matchResponse(response, 400, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.BadRequest$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 401, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Unauthorized$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 403, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Forbidden$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 404, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.NotFound$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 409, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Conflict$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 410, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.InviteExpired$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 422, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.UnprocessableEntity$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 429, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.RateLimitExceeded$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 500, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.InternalServerError$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else {
const responseBody = await response.text();
throw new errors.SDKError(
"Unexpected API response status or content-type",
response,
responseBody
);
}
}
/**
* Retrieve a workspace
*
* @remarks
* Retrieve a workspace for the authenticated user.
*/
async get(
input: operations.GetWorkspaceRequest,
options?: RequestOptions
): Promise<components.WorkspaceSchema> {
const headers$ = new Headers();
headers$.set("user-agent", SDK_METADATA.userAgent);
headers$.set("Accept", "application/json");
const payload$ = schemas$.parse(
input,
(value$) => operations.GetWorkspaceRequest$.outboundSchema.parse(value$),
"Input validation failed"
);
const body$ = null;
const pathParams$ = {
idOrSlug: enc$.encodeSimple("idOrSlug", payload$.idOrSlug, {
explode: false,
charEncoding: "percent",
}),
};
const path$ = this.templateURLComponent("/workspaces/{idOrSlug}")(pathParams$);
const query$ = "";
let security$;
if (typeof this.options$.token === "function") {
security$ = { token: await this.options$.token() };
} else if (this.options$.token) {
security$ = { token: this.options$.token };
} else {
security$ = {};
}
const context = {
operationID: "getWorkspace",
oAuth2Scopes: [],
securitySource: this.options$.token,
};
const securitySettings$ = this.resolveGlobalSecurity(security$);
const doOptions = {
context,
errorCodes: [
"400",
"401",
"403",
"404",
"409",
"410",
"422",
"429",
"4XX",
"500",
"5XX",
],
};
const request = this.createRequest$(
context,
{
security: securitySettings$,
method: "GET",
path: path$,
headers: headers$,
query: query$,
body: body$,
},
options
);
const response = await this.do$(request, doOptions);
const responseFields$ = {
HttpMeta: {
Response: response,
Request: request,
},
};
if (this.matchResponse(response, 200, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return components.WorkspaceSchema$.inboundSchema.parse(val$);
},
"Response validation failed"
);
return result;
} else if (this.matchResponse(response, 400, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.BadRequest$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 401, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Unauthorized$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 403, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Forbidden$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 404, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.NotFound$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 409, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.Conflict$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 410, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.InviteExpired$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 422, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.UnprocessableEntity$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 429, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.RateLimitExceeded$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else if (this.matchResponse(response, 500, "application/json")) {
const responseBody = await response.json();
const result = schemas$.parse(
responseBody,
(val$) => {
return errors.InternalServerError$.inboundSchema.parse({
...responseFields$,
...val$,
});
},
"Response validation failed"
);
throw result;
} else {
const responseBody = await response.text();
throw new errors.SDKError(
"Unexpected API response status or content-type",
response,
responseBody
);
}
}
}