Skip to content

Commit

Permalink
feat: use new routes (#66)
Browse files Browse the repository at this point in the history
* release: v2.8.1

* feat: use new routes

* feat: rename organizations rout to 'orgs'

* feat: use new routes for secrets

* feat: update deploy api

---------

Co-authored-by: mo4islona <[email protected]>
  • Loading branch information
belopash and mo4islona authored Feb 24, 2024
1 parent ea06ddf commit 41eacc9
Show file tree
Hide file tree
Showing 23 changed files with 295 additions and 219 deletions.
12 changes: 10 additions & 2 deletions src/api/alias.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { api } from './api';
import { HttpResponse, SquidResponse } from './types';

export async function setProduction(squidName: string, versionName: string): Promise<SquidResponse> {
export async function setProduction({
orgCode,
squidName,
versionName,
}: {
orgCode: string;
squidName: string;
versionName: string;
}): Promise<SquidResponse> {
const { body } = await api<HttpResponse<SquidResponse>>({
method: 'put',
path: `/aliases/squid/${squidName}/versions/${versionName}/prod`,
path: `/orgs/${orgCode}/squids/${squidName}/versions/${versionName}/prod`,
});

return body.payload;
Expand Down
14 changes: 10 additions & 4 deletions src/api/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ import qs from 'qs';
import { api } from './api';
import { DeployResponse, HttpResponse } from './types';

export async function getDeploy(id: string): Promise<DeployResponse> {
export async function getDeploy({ orgCode, id }: { orgCode: string; id: string }): Promise<DeployResponse> {
const { body } = await api<HttpResponse<DeployResponse>>({
method: 'get',
path: `/deploys/${id}`,
path: `/orgs/${orgCode}/deploys/${id}`,
});

return body.payload;
}

export async function getDeploys(query: { versionId: number }): Promise<DeployResponse[]> {
export async function getDeploys({
orgCode,
query,
}: {
orgCode: string;
query: { versionId: number };
}): Promise<DeployResponse[]> {
const { body } = await api<HttpResponse<DeployResponse[]>>({
method: 'get',
path: `/deploys/?${qs.stringify(query)}`,
path: `/orgs/${orgCode}/deploys/?${qs.stringify(query)}`,
});

return body.payload;
Expand Down
27 changes: 7 additions & 20 deletions src/api/secrets.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import { api } from './api';
import { HttpResponse, SecretsListResponse } from './types';

export async function listSecrets({ organization }: { organization?: string }): Promise<SecretsListResponse> {
export async function listSecrets({ orgCode }: { orgCode: string }): Promise<SecretsListResponse> {
const { body } = await api<HttpResponse<SecretsListResponse>>({
method: 'get',
query: organization
? {
organization,
}
: undefined,
path: `/secrets`,
path: `/orgs/${orgCode}/secrets`,
});
return body.payload;
}

export async function removeSecret({
name,
organization,
}: {
name: string;
organization?: string;
}): Promise<SecretsListResponse> {
export async function removeSecret({ name, orgCode }: { name: string; orgCode: string }): Promise<SecretsListResponse> {
const { body } = await api<HttpResponse<SecretsListResponse>>({
method: 'put',
path: `/secrets`,
path: `/orgs/${orgCode}/secrets`,
data: {
organization,
secrets: [{ action: 'DELETE', name }],
},
});
Expand All @@ -36,17 +24,16 @@ export async function removeSecret({
export async function setSecret({
name,
value,
organization,
orgCode,
}: {
name: string;
value: string;
organization?: string;
orgCode: string;
}): Promise<SecretsListResponse> {
const { body } = await api<HttpResponse<SecretsListResponse>>({
method: 'put',
path: `/secrets`,
path: `/orgs/${orgCode}/secrets`,
data: {
organization,
secrets: [{ action: 'UPDATE', name, value }],
},
});
Expand Down
163 changes: 90 additions & 73 deletions src/api/squids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,27 @@ import {
VersionResponse,
} from './types';

export async function squidList({ organization }: { organization?: string } = {}): Promise<SquidResponse[]> {
export async function squidList({ orgCode }: { orgCode: string }): Promise<SquidResponse[]> {
const { body } = await api<HttpResponse<SquidResponse[]>>({
method: 'get',
path: '/squids',
query: {
organization,
},
path: `/orgs/${orgCode}/squids`,
});

return body.payload;
}

export async function getSquid({
orgCode,
squidName,
versionName,
}: {
orgCode: string;
squidName: string;
versionName?: string;
}): Promise<SquidResponse> {
const { body } = await api<HttpResponse<SquidResponse>>({
method: 'get',
path: `/squids/${squidName}`,
path: `/orgs/${orgCode}/squids/${squidName}`,
query: {
versionName,
},
Expand All @@ -54,22 +53,29 @@ export async function squidNameIsAvailable(squidName: string): Promise<boolean>
return Boolean(body.payload?.available);
}

export async function versionHistoryLogs(
squidName: string,
versionName: string,
export async function versionHistoryLogs({
orgCode,
squidName,
versionName,
query,
abortController,
}: {
orgCode: string;
squidName: string;
versionName: string;
query: {
limit: number;
from: Date;
nextPage?: string;
orderBy?: string;
container?: string[];
level?: string[];
},
{ abortController }: { abortController?: AbortController } = {},
): Promise<LogsResponse> {
};
abortController?: AbortController;
}): Promise<LogsResponse> {
const { body } = await api<LogsResponse>({
method: 'get',
path: `/client/squid/${squidName}/versions/${versionName}/logs/history`,
path: `/orgs/${orgCode}/squids/${squidName}/versions/${versionName}/logs/history`,
query: {
...query,
from: query.from.toISOString(),
Expand All @@ -81,15 +87,22 @@ export async function versionHistoryLogs(
return body || { logs: [], nextPage: null };
}

export async function versionLogsFollow(
squidName: string,
versionName: string,
query: { container?: string[]; level?: string[] },
abortController?: AbortController,
) {
export async function versionLogsFollow({
orgCode,
query,
squidName,
versionName,
abortController,
}: {
orgCode: string;
squidName: string;
versionName: string;
query: { container?: string[]; level?: string[] };
abortController?: AbortController;
}) {
const { body } = await api<NodeJS.ReadableStream>({
method: 'get',
path: `/client/squid/${squidName}/versions/${versionName}/logs/follow`,
path: `/orgs/${orgCode}/squids/${squidName}/versions/${versionName}/logs/follow`,
query,
responseType: 'stream',
abortController: abortController,
Expand All @@ -98,21 +111,35 @@ export async function versionLogsFollow(
return body;
}

export async function streamSquidLogs(
squidName: string,
versionName: string,
onLog: (log: string) => unknown,
query: { container?: string[]; level?: string[] } = {},
{ abortController }: { abortController?: AbortController } = {},
) {
export async function streamSquidLogs({
orgCode,
squidName,
versionName,
abortController,
query = {},
onLog,
}: {
orgCode: string;
squidName: string;
versionName: string;
onLog: (log: string) => unknown;
query?: { container?: string[]; level?: string[] };
abortController?: AbortController;
}) {
let attempt = 0;
let stream: NodeJS.ReadableStream;

while (true) {
debugLog(`streaming logs`);
const retry = await new Promise(async (resolve) => {
try {
stream = await versionLogsFollow(squidName, versionName, query, abortController);
stream = await versionLogsFollow({
orgCode,
squidName,
versionName,
query,
abortController,
});
} catch (e: any) {
/**
* 524 status means timeout
Expand Down Expand Up @@ -168,83 +195,73 @@ export async function streamSquidLogs(
}
}

export async function releaseSquid(
squidName: string,
versionName: string,
artifactUrl: string,
description?: string,
envs?: Record<string, string>,
): Promise<SquidVersionResponse> {
const { body } = await api<SquidVersionResponse>({
method: 'post',
path: `/client/squid/${squidName}/version`,
data: { artifactUrl, versionName, description, envs },
});

return body;
}

export async function deploySquid(data: {
hardReset: boolean;
artifactUrl: string;
manifestPath: string;
organization?: string;
export async function deploySquid({
orgCode,
data,
}: {
orgCode: string;
data: {
hardReset: boolean;
artifactUrl: string;
manifestPath: string;
};
}): Promise<DeployResponse> {
const { body } = await api<HttpResponse<DeployResponse>>({
method: 'post',
path: `/squids/deploy`,
path: `/orgs/${orgCode}/squids/deploy`,
data,
});

return body.payload;
}

export async function getUploadUrl(): Promise<UploadUrlResponse> {
export async function getUploadUrl({ orgCode }: { orgCode: string }): Promise<UploadUrlResponse> {
const { body } = await api<HttpResponse<UploadUrlResponse>>({
method: 'post',
path: `/deploys/upload-url`,
path: `/orgs/${orgCode}/deploys/upload-url`,
});

return body.payload;
}

export async function updateSquid(
squidName: string,
versionName: string,
artifactUrl: string,
hardReset: boolean,
envs?: Record<string, string>,
): Promise<VersionResponse> {
const { body } = await api<VersionResponse>({
method: 'put',
path: `/client/squid/${squidName}/version/${versionName}/deployment`,
data: { artifactUrl, hardReset, envs },
});
return body;
}

export async function restartSquid(squidName: string, versionName: string): Promise<DeployResponse> {
export async function restartSquid({
orgCode,
squidName,
versionName,
}: {
orgCode: string;
squidName: string;
versionName: string;
}): Promise<DeployResponse> {
const { body } = await api<HttpResponse<DeployResponse>>({
method: 'put',
path: `/squids/${squidName}/version/${versionName}/redeploy`,
path: `/orgs/${orgCode}/squids/${squidName}/version/${versionName}/redeploy`,
});

return body.payload;
}

export async function destroyVersion(squidName: string, versionName: string): Promise<string> {
export async function destroyVersion({
orgCode,
squidName,
versionName,
}: {
orgCode: string;
squidName: string;
versionName: string;
}): Promise<string> {
const { body } = await api<HttpResponse<SquidResponse>>({
method: 'delete',
path: `/squids/${squidName}/version/${versionName}`,
path: `/orgs/${orgCode}/squids/${squidName}/version/${versionName}`,
});

return `Destroyed Squid "${body.payload.name}" version ${body.payload.versions?.[0]?.name}`;
}

export async function destroySquid(squidName: string): Promise<string> {
export async function destroySquid({ orgCode, squidName }: { orgCode: string; squidName: string }): Promise<string> {
const { body } = await api<HttpResponse<SquidResponse>>({
method: 'delete',
path: `/squids/${squidName}`,
path: `/orgs/${orgCode}/squids/${squidName}`,
});

return `Destroyed Squid ${body.payload.name}`;
Expand Down
1 change: 1 addition & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type DeployResponse = {
status: DeployStatus;
squidName?: string;
versionName?: string;
orgCode?: string;
deploymentUrl?: string;
failed: 'NO' | 'UNEXPECTED' | 'PERMISSIONS' | 'REQUIREMENTS' | 'SOURCE_FILES_BUILD_FAILED';
logs: { severity: 'debug' | 'warn' | 'info' | 'error'; message: string }[];
Expand Down
4 changes: 2 additions & 2 deletions src/api/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import fetch from 'node-fetch';
import { ApiError } from './api';
import { getUploadUrl } from './squids';

export async function uploadFile(path: string): Promise<{ error: string | null; fileUrl?: string }> {
const { uploadFields, uploadUrl, maxUploadBytes, fileUrl } = await getUploadUrl();
export async function uploadFile(orgCode: string, path: string): Promise<{ error: string | null; fileUrl?: string }> {
const { uploadFields, uploadUrl, maxUploadBytes, fileUrl } = await getUploadUrl({ orgCode });

const fileStream = fs.createReadStream(path);
const { size } = fs.statSync(path);
Expand Down
Loading

0 comments on commit 41eacc9

Please sign in to comment.