diff --git a/src/auth-request.ts b/src/auth-request.ts index e6f70c6..1967f20 100644 --- a/src/auth-request.ts +++ b/src/auth-request.ts @@ -38,6 +38,7 @@ export function request({ discoConfig, body, expectedStatuses = [200], + extraHeaders, bodyStream, }: { method: string @@ -45,6 +46,7 @@ export function request({ discoConfig: DiscoConfig body?: unknown expectedStatuses?: number[] + extraHeaders?: Record, bodyStream?: Readable }) { const params: RequestInit = { @@ -60,9 +62,17 @@ export function request({ ...params.headers, 'Content-Type': 'application/json', } + params.body = JSON.stringify(body) } + if (extraHeaders !== undefined) { + params.headers = { + ...params.headers, + ...extraHeaders, + } + } + if (bodyStream) { params.body = bodyStream } diff --git a/src/commands/postgres/addon/install.ts b/src/commands/postgres/addon/install.ts index c92b8bb..781d3a0 100644 --- a/src/commands/postgres/addon/install.ts +++ b/src/commands/postgres/addon/install.ts @@ -15,7 +15,7 @@ export default class PostgresAddonInstall extends Command { const {flags} = await this.parse(PostgresAddonInstall) const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects` - const project = 'postgres-addon'; + const project = 'postgres-addon' const body = { name: project, githubRepo: 'letsdiscodev/disco-addon-postgres', diff --git a/src/commands/postgres/addon/remove.ts b/src/commands/postgres/addon/remove.ts index e928749..6830f0a 100644 --- a/src/commands/postgres/addon/remove.ts +++ b/src/commands/postgres/addon/remove.ts @@ -14,7 +14,7 @@ export default class PostgresAddonRemove extends Command { public async run(): Promise { const {flags} = await this.parse(PostgresAddonRemove) const discoConfig = getDisco(flags.disco || null) - const project = 'postgres-addon'; + const project = 'postgres-addon' const url = `https://${discoConfig.host}/api/projects/${project}` try { await request({method: 'DELETE', url, discoConfig, expectedStatuses: [200, 204]}) diff --git a/src/commands/postgres/addon/update.ts b/src/commands/postgres/addon/update.ts index b58b1ff..ad9c74b 100644 --- a/src/commands/postgres/addon/update.ts +++ b/src/commands/postgres/addon/update.ts @@ -14,7 +14,7 @@ export default class PostgresAddonUpdate extends Command { public async run(): Promise { const {flags} = await this.parse(PostgresAddonUpdate) const discoConfig = getDisco(flags.disco || null) - const project = 'postgres-addon'; + const project = 'postgres-addon' const url = `https://${discoConfig.host}/api/projects/${project}/deployments` const res = await request({method: 'POST', url, body: {}, discoConfig, expectedStatuses: [201]}) const data = (await res.json()) as any diff --git a/src/commands/postgres/databases/add.ts b/src/commands/postgres/databases/add.ts index 246a932..6e8c00e 100644 --- a/src/commands/postgres/databases/add.ts +++ b/src/commands/postgres/databases/add.ts @@ -21,7 +21,15 @@ export default class PostgresDatabasesAdd extends Command { const {flags} = await this.parse(PostgresDatabasesAdd) const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases` - const res = await request({method: 'POST', url, discoConfig, expectedStatuses: [201]}) + const res = await request({ + method: 'POST', + url, + discoConfig, + expectedStatuses: [201], + extraHeaders: { + 'X-Disco-Include-API-Key': 'true', + }, + }) const respBody = (await res.json()) as {database: PostgresDatabase} this.log(respBody.database.name) } diff --git a/src/commands/postgres/databases/attach.ts b/src/commands/postgres/databases/attach.ts index fa0b1db..cf0c289 100644 --- a/src/commands/postgres/databases/attach.ts +++ b/src/commands/postgres/databases/attach.ts @@ -20,6 +20,15 @@ export default class PostgresDatabasesAttach extends Command { const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases/${flags.database}/attach` const reqBody = {envVar: flags['env-var'], project: flags.project} - await request({method: 'POST', url, discoConfig, body: reqBody, expectedStatuses: [200]}) + await request({ + method: 'POST', + url, + discoConfig, + body: reqBody, + expectedStatuses: [200], + extraHeaders: { + 'X-Disco-Include-API-Key': 'true', + }, + }) } } diff --git a/src/commands/postgres/databases/detach.ts b/src/commands/postgres/databases/detach.ts index dd67684..ccc4cd8 100644 --- a/src/commands/postgres/databases/detach.ts +++ b/src/commands/postgres/databases/detach.ts @@ -20,6 +20,15 @@ export default class PostgresDatabasesDetach extends Command { const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases/${flags.database}/detach` const reqBody = {envVar: flags['env-var'], project: flags.project} - await request({method: 'POST', url, discoConfig, body: reqBody, expectedStatuses: [200]}) + await request({ + method: 'POST', + url, + discoConfig, + body: reqBody, + expectedStatuses: [200], + extraHeaders: { + 'X-Disco-Include-API-Key': 'true', + }, + }) } } diff --git a/src/commands/postgres/databases/list.ts b/src/commands/postgres/databases/list.ts index 62e23fc..8d75153 100644 --- a/src/commands/postgres/databases/list.ts +++ b/src/commands/postgres/databases/list.ts @@ -3,8 +3,8 @@ import {getDisco} from '../../../config.js' import {request} from '../../../auth-request.js' type PostgresDatabase = { - created: string; - name: string; + created: string + name: string } export default class PostgresDatabasesList extends Command { @@ -21,7 +21,15 @@ export default class PostgresDatabasesList extends Command { const {flags} = await this.parse(PostgresDatabasesList) const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases` - const res = await request({method: 'GET', url, discoConfig, expectedStatuses: [200]}) + const res = await request({ + method: 'GET', + url, + discoConfig, + expectedStatuses: [200], + extraHeaders: { + 'X-Disco-Include-API-Key': 'true', + }, + }) const respBody = (await res.json()) as {databases: PostgresDatabase[]} for (const database of respBody.databases) { this.log(database.name) diff --git a/src/commands/postgres/databases/remove.ts b/src/commands/postgres/databases/remove.ts index 89a6589..0859aae 100644 --- a/src/commands/postgres/databases/remove.ts +++ b/src/commands/postgres/databases/remove.ts @@ -17,6 +17,14 @@ export default class PostgresDatabasesRemove extends Command { const {flags} = await this.parse(PostgresDatabasesRemove) const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}/databases/${flags.database}` - await request({method: 'DELETE', url, discoConfig, expectedStatuses: [200]}) + await request({ + method: 'DELETE', + url, + discoConfig, + expectedStatuses: [200], + extraHeaders: { + 'X-Disco-Include-API-Key': 'true', + }, + }) } } diff --git a/src/commands/postgres/instances/add.ts b/src/commands/postgres/instances/add.ts index 93ac02f..77e1e7a 100644 --- a/src/commands/postgres/instances/add.ts +++ b/src/commands/postgres/instances/add.ts @@ -15,7 +15,15 @@ export default class PostgresInstancesAdd extends Command { const {flags} = await this.parse(PostgresInstancesAdd) const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances` - const res = await request({method: 'POST', url, discoConfig, expectedStatuses: [201]}) + const res = await request({ + method: 'POST', + url, + discoConfig, + expectedStatuses: [201], + extraHeaders: { + 'X-Disco-Include-API-Key': 'true', + }, + }) const respBody = (await res.json()) as {project: {name: string}; deployment: {number: number}} this.log(`Added instance ${respBody.project.name}`) const deploymentUrl = `https://${discoConfig.host}/api/projects/${respBody.project.name}/deployments/${respBody.deployment.number}/output` diff --git a/src/commands/postgres/instances/list.ts b/src/commands/postgres/instances/list.ts index 1e6b816..5384d20 100644 --- a/src/commands/postgres/instances/list.ts +++ b/src/commands/postgres/instances/list.ts @@ -3,7 +3,7 @@ import {getDisco} from '../../../config.js' import {request} from '../../../auth-request.js' type PostgresInstance = { - name: string; + name: string } export default class PostgresInstancesList extends Command { @@ -19,7 +19,15 @@ export default class PostgresInstancesList extends Command { const {flags} = await this.parse(PostgresInstancesList) const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances` - const res = await request({method: 'GET', url, discoConfig, expectedStatuses: [200]}) + const res = await request({ + method: 'GET', + url, + discoConfig, + expectedStatuses: [200], + extraHeaders: { + 'X-Disco-Include-API-Key': 'true', + }, + }) const respBody = (await res.json()) as {instances: PostgresInstance[]} for (const instance of respBody.instances) { this.log(instance.name) diff --git a/src/commands/postgres/instances/remove.ts b/src/commands/postgres/instances/remove.ts index be59a58..ab76235 100644 --- a/src/commands/postgres/instances/remove.ts +++ b/src/commands/postgres/instances/remove.ts @@ -16,6 +16,14 @@ export default class PostgresInstancesRemove extends Command { const {flags} = await this.parse(PostgresInstancesRemove) const discoConfig = getDisco(flags.disco || null) const url = `https://${discoConfig.host}/api/projects/postgres-addon/cgi/endpoints/instances/${flags.instance}` - await request({method: 'DELETE', url, discoConfig, expectedStatuses: [200]}) + await request({ + method: 'DELETE', + url, + discoConfig, + expectedStatuses: [200], + extraHeaders: { + 'X-Disco-Include-API-Key': 'true', + }, + }) } }