@heroku/mcp-server 0.0.1-alpha.0

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.
Files changed (42) hide show
  1. package/LICENSE.txt +206 -0
  2. package/README.md +279 -0
  3. package/dist/index.js +86 -0
  4. package/dist/repl/heroku-cli-repl.js +210 -0
  5. package/dist/repl/heroku-cli-repl.spec.js +106 -0
  6. package/dist/services/app-service.js +42 -0
  7. package/dist/services/app-setup-service.js +72 -0
  8. package/dist/services/build-service.js +149 -0
  9. package/dist/services/source-service.js +42 -0
  10. package/dist/tools/addons.js +241 -0
  11. package/dist/tools/addons.spec.js +335 -0
  12. package/dist/tools/apps.js +222 -0
  13. package/dist/tools/apps.spec.js +332 -0
  14. package/dist/tools/data.js +345 -0
  15. package/dist/tools/data.spec.js +300 -0
  16. package/dist/tools/deploy-to-heroku.js +449 -0
  17. package/dist/tools/deploy-to-heroku.spec.js +166 -0
  18. package/dist/tools/logs.js +61 -0
  19. package/dist/tools/logs.spec.js +112 -0
  20. package/dist/tools/maintenance.js +55 -0
  21. package/dist/tools/maintenance.spec.js +115 -0
  22. package/dist/tools/pipelines.js +146 -0
  23. package/dist/tools/pipelines.spec.js +183 -0
  24. package/dist/tools/ps.js +99 -0
  25. package/dist/tools/ps.spec.js +127 -0
  26. package/dist/tools/spaces.js +34 -0
  27. package/dist/tools/spaces.spec.js +71 -0
  28. package/dist/tools/teams.js +37 -0
  29. package/dist/tools/teams.spec.js +69 -0
  30. package/dist/utils/app-json.schema.json +232 -0
  31. package/dist/utils/command-builder.js +61 -0
  32. package/dist/utils/command-builder.spec.js +95 -0
  33. package/dist/utils/generate-request-init.js +18 -0
  34. package/dist/utils/handle-cli-output.js +26 -0
  35. package/dist/utils/handle-cli-output.spec.js +54 -0
  36. package/dist/utils/mcp-tool-response.js +1 -0
  37. package/dist/utils/read-app-json.js +39 -0
  38. package/dist/utils/read-app-json.spec.js +106 -0
  39. package/dist/utils/tarball.js +114 -0
  40. package/dist/utils/tarball.spec.js +98 -0
  41. package/dist/utils/tool-commands.js +47 -0
  42. package/package.json +85 -0
@@ -0,0 +1,210 @@
1
+ import { spawn } from 'node:child_process';
2
+ import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+ const heroku = path.parse(path.relative(process.cwd(), fileURLToPath(import.meta.resolve('heroku', import.meta.url))));
5
+ const herokuRunPath = path.join(heroku.dir, '../', 'bin/run');
6
+ const COMMAND_END_RESULTS_MESSAGE = '<<<END RESULTS>>>';
7
+ const READY_MESSAGE = 'heroku >';
8
+ /**
9
+ * A class that manages a Heroku CLI REPL process
10
+ * and allows for durably executing commands via a queue.
11
+ *
12
+ * Durability is achieved by guaranteeing that commands are
13
+ * executed in order and that the REPL process is restarted
14
+ * if it exits unexpectedly, experiences an error, or times out
15
+ * during command execution.
16
+ *
17
+ * A command that fails to complete will not be retried but
18
+ * all output leading up to the failure is returned to the caller
19
+ * and the REPL process is restarted. Any commands that remained in
20
+ * the queue prior to the error will not be affected and execution
21
+ * will continue once the new process is ready for input.
22
+ *
23
+ * If the REPL process is aborted or disposed of intentionally,
24
+ * it will not be restarted and a new instance should be created.
25
+ *
26
+ * This maintains a single Heroku CLI process.
27
+ */
28
+ export class HerokuREPL {
29
+ static spawn = spawn;
30
+ isProcessingCommand = false;
31
+ isReady = false;
32
+ process;
33
+ buffer = '';
34
+ commandQueue = [];
35
+ abortController = new AbortController();
36
+ pauseIterator;
37
+ pauseIteratorResolver;
38
+ commandTimeout;
39
+ commandTimeoutId;
40
+ /**
41
+ * Create a new HerokuREPL instance
42
+ *
43
+ * @param commandTimeout the timeout for command execution
44
+ */
45
+ constructor(commandTimeout = 15_000) {
46
+ this.commandTimeout = commandTimeout;
47
+ this.pauseQueue();
48
+ void this.initializeProcess();
49
+ this.abortController.signal.addEventListener('abort', this.onAbort);
50
+ }
51
+ /**
52
+ * Clean up the process when the instance is disposed
53
+ */
54
+ [Symbol.dispose]() {
55
+ this.abortController.abort();
56
+ }
57
+ /**
58
+ * async iterator that yields the commands being executed
59
+ *
60
+ * @returns An async iterator that yields the commands being executed
61
+ * @yields the command being executed
62
+ */
63
+ async *[Symbol.asyncIterator]() {
64
+ while (!this.abortController.signal.aborted) {
65
+ await this.pauseIterator;
66
+ const [nextCommand] = this.commandQueue;
67
+ if (!nextCommand || !this.isReady) {
68
+ this.pauseQueue();
69
+ continue;
70
+ }
71
+ this.buffer = '';
72
+ this.isProcessingCommand = true;
73
+ this.process?.stdin?.write(nextCommand.command + '\n');
74
+ this.commandTimeoutId = setTimeout(() => {
75
+ // If a timeout occurs, we need to restart the process
76
+ // in case it's in a bad state or an error occurred
77
+ // that we can't recover from
78
+ void this.initializeProcess();
79
+ this.handleOutput(`The command failed to complete in ${this.commandTimeout}ms\n${COMMAND_END_RESULTS_MESSAGE}\n`);
80
+ }, this.commandTimeout);
81
+ yield nextCommand.promise;
82
+ }
83
+ }
84
+ /**
85
+ * Adds a command to the queue for processing
86
+ *
87
+ * @param command the full command to execute in the Heroku CLI including all flags and args
88
+ * @returns a promise that resolves when the command is complete
89
+ */
90
+ async executeCommand(command) {
91
+ if (this.abortController.signal.aborted) {
92
+ throw new Error('REPL process has been aborted and cannot execute commands');
93
+ }
94
+ // Replace with Promise.withResolvers once we move to node 22
95
+ let resolver;
96
+ const commandPromise = new Promise((resolve) => {
97
+ resolver = resolve;
98
+ });
99
+ this.commandQueue.push({ command, promise: commandPromise, resolver });
100
+ this.pauseIteratorResolver?.();
101
+ return commandPromise;
102
+ }
103
+ /**
104
+ * Initializes the Heroku CLI process in REPL mode and sets up
105
+ * event listeners for stdout and stderr.
106
+ *
107
+ * This method is called when the instance is created
108
+ * and when the process is closed earlier than expected.
109
+ * If the process is aborted or dispose is called,
110
+ * REPL will be killed and it will not be restarted.
111
+ */
112
+ initializeProcess() {
113
+ this.isReady = false;
114
+ if (this.abortController.signal.aborted) {
115
+ return;
116
+ }
117
+ if (this.process) {
118
+ this.process.removeAllListeners();
119
+ this.process.kill();
120
+ this.process = undefined;
121
+ }
122
+ this.process = HerokuREPL.spawn(herokuRunPath, [], {
123
+ stdio: ['pipe', 'pipe', 'pipe'],
124
+ signal: this.abortController.signal,
125
+ env: {
126
+ ...process.env,
127
+ HEROKU_MCP_MODE: 'true'
128
+ }
129
+ });
130
+ this.process.stdout?.on('data', (data) => {
131
+ const output = data.toString();
132
+ if (output.includes(READY_MESSAGE)) {
133
+ this.isReady = true;
134
+ this.pauseIteratorResolver?.();
135
+ }
136
+ this.handleOutput(output);
137
+ });
138
+ // We're capturing this data and sending it along
139
+ // to subscribers because the CLI does provide
140
+ // information on this stream. Output here does
141
+ // not always indicate an error has occurred
142
+ this.process.stderr?.on('data', (data) => {
143
+ this.buffer += data.toString().trim();
144
+ });
145
+ // If the process was aborted, don't restart it
146
+ // Aborting the process is handled by the dispose method
147
+ // and is the only reason the process should be killed
148
+ this.process.on('close', (code) => {
149
+ if (this.abortController.signal.aborted) {
150
+ return;
151
+ }
152
+ this.pauseQueue();
153
+ // If the process was closed unexpectedly, restart it
154
+ // Otherwise, wait for a second and try again
155
+ if (code !== 0) {
156
+ // We can assume the process was closed unexpectedly
157
+ // and we should attempt to determine the cause
158
+ // and pass this info back to the LLM for processing
159
+ // along with the output
160
+ this.buffer += `\n\nHeroku CLI process closed unexpectedly with code ${code}. Restarting...`;
161
+ void this.initializeProcess();
162
+ }
163
+ else {
164
+ // The process was closed normally but this could be
165
+ // from a rogue command or a user aborting the parent process
166
+ setTimeout(() => void this.initializeProcess(), 1000);
167
+ }
168
+ });
169
+ }
170
+ /**
171
+ * Handles the output from the Heroku CLI process.
172
+ *
173
+ * @param output The output from the Heroku CLI process
174
+ */
175
+ handleOutput(output) {
176
+ this.buffer += output;
177
+ // Check if command execution is complete
178
+ if (this.buffer.includes(COMMAND_END_RESULTS_MESSAGE) && this.isProcessingCommand) {
179
+ const result = this.buffer.trim();
180
+ this.buffer = '';
181
+ this.isProcessingCommand = false;
182
+ clearTimeout(this.commandTimeoutId);
183
+ const currentCommand = this.commandQueue.shift();
184
+ if (currentCommand) {
185
+ currentCommand.resolver(result);
186
+ }
187
+ if (this.commandQueue.length === 0) {
188
+ this.pauseQueue();
189
+ }
190
+ }
191
+ }
192
+ /**
193
+ * Waits for the next command to be added to the queue
194
+ * or for the REPL to be ready for input
195
+ */
196
+ pauseQueue() {
197
+ // Replace with Promise.withResolvers once we move to node 22
198
+ this.pauseIterator = new Promise((resolve) => {
199
+ this.pauseIteratorResolver = resolve;
200
+ });
201
+ }
202
+ /**
203
+ * Handles the abort event
204
+ */
205
+ onAbort = () => {
206
+ this.commandQueue.length = 0;
207
+ this.pauseIteratorResolver?.();
208
+ clearTimeout(this.commandTimeoutId);
209
+ };
210
+ }
@@ -0,0 +1,106 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import { expect } from 'chai';
3
+ import sinon from 'sinon';
4
+ import { HerokuREPL } from './heroku-cli-repl.js';
5
+ describe('HerokuREPL', () => {
6
+ let repl;
7
+ let mockProcess;
8
+ let spawnStub;
9
+ beforeEach(() => {
10
+ // Create mock process with stdin, stdout, and stderr streams
11
+ mockProcess = new (class extends EventEmitter {
12
+ stdin = new EventEmitter();
13
+ stdout = new EventEmitter();
14
+ stderr = new EventEmitter();
15
+ kill = sinon.stub();
16
+ constructor() {
17
+ super();
18
+ this.stdin.write = sinon.stub();
19
+ }
20
+ })();
21
+ // Stub the spawn function
22
+ spawnStub = sinon.stub(HerokuREPL, 'spawn').returns(mockProcess);
23
+ repl = new HerokuREPL(500);
24
+ mockProcess.stdout.emit('data', 'heroku >');
25
+ (async () => {
26
+ for await (const command of repl) {
27
+ // debug here if needed
28
+ }
29
+ })();
30
+ });
31
+ afterEach(() => {
32
+ repl[Symbol.dispose]();
33
+ sinon.restore();
34
+ });
35
+ describe('initialization', () => {
36
+ it('should create a new process when initialized', () => {
37
+ expect(spawnStub.calledOnce).to.be.true;
38
+ expect(spawnStub.firstCall.args[2].env.HEROKU_MCP_MODE).to.equal('true');
39
+ });
40
+ });
41
+ describe('executeCommand', () => {
42
+ it('should queue and execute commands', async () => {
43
+ const commandPromise = repl.executeCommand('apps');
44
+ // Simulate process output
45
+ setTimeout(() => mockProcess.stdout.emit('data', 'Command output\n<<<END RESULTS>>>'));
46
+ const result = await commandPromise;
47
+ expect(result).to.include('Command output');
48
+ });
49
+ it('should handle multiple commands in sequence', async () => {
50
+ const command1Promise = repl.executeCommand('apps');
51
+ const command2Promise = repl.executeCommand('addons');
52
+ setTimeout(() => mockProcess.stdout.emit('data', 'Command 1 output\n<<<END RESULTS>>>'));
53
+ setTimeout(() => mockProcess.stdout.emit('data', 'Command 2 output\n<<<END RESULTS>>>'));
54
+ const result1 = await command1Promise;
55
+ const result2 = await command2Promise;
56
+ expect(result1).to.include('Command 1 output');
57
+ expect(result2).to.include('Command 2 output');
58
+ });
59
+ it('should capture stderr output', async () => {
60
+ const commandPromise = repl.executeCommand('apps');
61
+ setTimeout(() => mockProcess.stderr.emit('data', 'Warning message'));
62
+ setTimeout(() => mockProcess.stdout.emit('data', 'Command output\n<<<END RESULTS>>>'));
63
+ const result = await commandPromise;
64
+ expect(result).to.include('Warning message');
65
+ });
66
+ it('should timeout if command takes too long', async () => {
67
+ const commandPromise = repl.executeCommand('apps');
68
+ setTimeout(() => mockProcess.stdout.emit('data', 'This is taking too long...\n'), 600);
69
+ const result = await commandPromise;
70
+ expect(result).to.include('The command failed to complete in 500ms');
71
+ });
72
+ });
73
+ describe('process management', () => {
74
+ it('should restart process on unexpected close', () => {
75
+ mockProcess.emit('close', 1);
76
+ expect(spawnStub.calledTwice).to.be.true;
77
+ });
78
+ it('should not restart process when aborted', () => {
79
+ repl[Symbol.dispose]();
80
+ mockProcess.emit('close', 0);
81
+ expect(spawnStub.calledOnce).to.be.true;
82
+ });
83
+ });
84
+ describe('async iterator', () => {
85
+ it('should yield command results as they are executed', async () => {
86
+ const commandPromise = repl.executeCommand('apps');
87
+ let commands = [];
88
+ void (async () => {
89
+ for await (const command of repl) {
90
+ commands.push(command);
91
+ }
92
+ })();
93
+ // Clean up the command
94
+ setTimeout(() => mockProcess.stdout.emit('data', 'Output\n<<<END RESULTS>>>'));
95
+ await commandPromise;
96
+ await new Promise((resolve) => setTimeout(resolve, 50));
97
+ expect(commands[0]).to.equal('Output\n<<<END RESULTS>>>');
98
+ });
99
+ });
100
+ describe('cleanup', () => {
101
+ it('should abort when disposed', () => {
102
+ repl[Symbol.dispose]();
103
+ expect(repl['abortController'].signal.aborted).to.be.true;
104
+ });
105
+ });
106
+ });
@@ -0,0 +1,42 @@
1
+ /**
2
+ * [Heroku Platform API - App](https://devcenter.heroku.com/articles/platform-api-reference#app)
3
+ * An app represents the program that you would like to deploy and run on Heroku.
4
+ */
5
+ export default class AppService {
6
+ endpoint;
7
+ /**
8
+ *
9
+ * @param endpoint The endpoint to use for the app service.
10
+ */
11
+ constructor(endpoint) {
12
+ this.endpoint = endpoint;
13
+ }
14
+ /**
15
+ * Info for existing app.
16
+ *
17
+ * @param appIdentity unique identifier of app or unique name of app.
18
+ * @param requestInit The initializer for the request.
19
+ * @returns The app.
20
+ */
21
+ async info(appIdentity, requestInit = {}) {
22
+ const response = await fetch(`${this.endpoint}/apps/${appIdentity}`, {
23
+ ...requestInit,
24
+ method: 'GET',
25
+ headers: {
26
+ ...requestInit?.headers,
27
+ Accept: 'application/vnd.heroku+json; version=3.sdk'
28
+ }
29
+ });
30
+ if (response.ok) {
31
+ return (await response.json());
32
+ }
33
+ let message = response.statusText;
34
+ try {
35
+ ({ message } = (await response.json()));
36
+ }
37
+ catch {
38
+ // no-op
39
+ }
40
+ throw new Error(`${response.status}: ${message}`, { cause: response });
41
+ }
42
+ }
@@ -0,0 +1,72 @@
1
+ /**
2
+ * [Heroku Setup API - App Setup](https://devcenter.heroku.com/articles/platform-api-reference#app-setup)
3
+ * An app setup represents an app on Heroku that is setup using an environment, addons, and scripts described in an app.json manifest file.
4
+ */
5
+ export default class AppSetupService {
6
+ endpoint;
7
+ /**
8
+ *
9
+ * @param endpoint The endpoint to use for the app setup service.
10
+ */
11
+ constructor(endpoint) {
12
+ this.endpoint = endpoint;
13
+ }
14
+ /**
15
+ * Create a new app setup from a gzipped tar archive containing an app.json manifest file.
16
+ *
17
+ * @param payload Object to send to the endpoint.
18
+ * @param requestInit The initializer for the request.
19
+ * @returns The app setup.
20
+ */
21
+ async create(payload, requestInit = {}) {
22
+ const response = await fetch(`${this.endpoint}/app-setups`, {
23
+ ...requestInit,
24
+ body: JSON.stringify(payload, null, 2),
25
+ method: 'POST',
26
+ headers: {
27
+ ...requestInit?.headers,
28
+ Accept: 'application/vnd.heroku+json; version=3.sdk',
29
+ 'Content-Type': 'application/json'
30
+ }
31
+ });
32
+ if (response.ok) {
33
+ return (await response.json());
34
+ }
35
+ let message = response.statusText;
36
+ try {
37
+ ({ message } = (await response.json()));
38
+ }
39
+ catch {
40
+ // no-op
41
+ }
42
+ throw new Error(`${response.status}: ${message}`, { cause: response });
43
+ }
44
+ /**
45
+ * Get the status of an app setup.
46
+ *
47
+ * @param appSetupIdentity unique identifier of app setup.
48
+ * @param requestInit The initializer for the request.
49
+ * @returns The app setup.
50
+ */
51
+ async info(appSetupIdentity, requestInit = {}) {
52
+ const response = await fetch(`${this.endpoint}/app-setups/${appSetupIdentity}`, {
53
+ ...requestInit,
54
+ method: 'GET',
55
+ headers: {
56
+ ...requestInit?.headers,
57
+ Accept: 'application/vnd.heroku+json; version=3.sdk'
58
+ }
59
+ });
60
+ if (response.ok) {
61
+ return (await response.json());
62
+ }
63
+ let message = response.statusText;
64
+ try {
65
+ ({ message } = (await response.json()));
66
+ }
67
+ catch {
68
+ // no-op
69
+ }
70
+ throw new Error(`${response.status}: ${message}`, { cause: response });
71
+ }
72
+ }
@@ -0,0 +1,149 @@
1
+ /**
2
+ * [Heroku Build API - Build](https://devcenter.heroku.com/articles/platform-api-reference#build)
3
+ * A build represents the process of transforming a code tarball into build artifacts
4
+ */
5
+ export default class BuildService {
6
+ endpoint;
7
+ /**
8
+ *
9
+ * @param endpoint The endpoint to use for the build service.
10
+ */
11
+ constructor(endpoint) {
12
+ this.endpoint = endpoint;
13
+ }
14
+ /**
15
+ * Create a new build.
16
+ *
17
+ * @param appIdentity unique identifier of app or unique name of app.
18
+ * @param payload Object to send to the endpoint.
19
+ * @param requestInit The initializer for the request.
20
+ * @returns The build.
21
+ */
22
+ async create(appIdentity, payload, requestInit = {}) {
23
+ const response = await fetch(`${this.endpoint}/apps/${appIdentity}/builds`, {
24
+ ...requestInit,
25
+ body: JSON.stringify(payload, null, 2),
26
+ method: 'POST',
27
+ headers: {
28
+ ...requestInit?.headers,
29
+ Accept: 'application/vnd.heroku+json; version=3.sdk',
30
+ 'Content-Type': 'application/json'
31
+ }
32
+ });
33
+ if (response.ok) {
34
+ return (await response.json());
35
+ }
36
+ let message = response.statusText;
37
+ try {
38
+ ({ message } = (await response.json()));
39
+ }
40
+ catch {
41
+ // no-op
42
+ }
43
+ throw new Error(`${response.status}: ${message}`, { cause: response });
44
+ }
45
+ /**
46
+ * Info for existing build.
47
+ *
48
+ * @param appIdentity unique identifier of app or unique name of app.
49
+ * @param buildIdentity unique identifier of build.
50
+ * @param requestInit The initializer for the request.
51
+ * @returns The build.
52
+ */
53
+ async info(appIdentity, buildIdentity, requestInit = {}) {
54
+ const response = await fetch(`${this.endpoint}/apps/${appIdentity}/builds/${buildIdentity}`, {
55
+ ...requestInit,
56
+ method: 'GET',
57
+ headers: {
58
+ ...requestInit?.headers,
59
+ Accept: 'application/vnd.heroku+json; version=3.sdk'
60
+ }
61
+ });
62
+ if (response.ok) {
63
+ return (await response.json());
64
+ }
65
+ let message = response.statusText;
66
+ try {
67
+ ({ message } = (await response.json()));
68
+ }
69
+ catch {
70
+ // no-op
71
+ }
72
+ throw new Error(`${response.status}: ${message}`, { cause: response });
73
+ }
74
+ /**
75
+ * List existing build.
76
+ *
77
+ * @param appIdentity unique identifier of app or unique name of app.
78
+ * @param requestInit The initializer for the request.
79
+ * @returns The builds.
80
+ */
81
+ async list(appIdentity, requestInit = {}) {
82
+ const response = await fetch(`${this.endpoint}/apps/${appIdentity}/builds`, {
83
+ ...requestInit,
84
+ method: 'GET',
85
+ headers: {
86
+ ...requestInit?.headers,
87
+ Accept: 'application/vnd.heroku+json; version=3.sdk'
88
+ }
89
+ });
90
+ if (response.ok) {
91
+ return (await response.json());
92
+ }
93
+ let message = response.statusText;
94
+ try {
95
+ ({ message } = (await response.json()));
96
+ }
97
+ catch {
98
+ // no-op
99
+ }
100
+ throw new Error(`${response.status}: ${message}`, { cause: response });
101
+ }
102
+ /**
103
+ * Destroy a build cache.
104
+ *
105
+ * @param appIdentity unique identifier of app or unique name of app.
106
+ * @param requestInit The initializer for the request.
107
+ */
108
+ async deleteCache(appIdentity, requestInit = {}) {
109
+ await fetch(`/apps/${appIdentity}/build-cache`, {
110
+ ...requestInit,
111
+ method: 'DELETE',
112
+ headers: {
113
+ ...requestInit?.headers,
114
+ Accept: 'application/vnd.heroku+json; version=3.sdk',
115
+ 'Content-Type': 'application/json'
116
+ }
117
+ });
118
+ }
119
+ /**
120
+ * Cancel running build.
121
+ *
122
+ * @param appIdentity unique identifier of app or unique name of app.
123
+ * @param buildIdentity unique identifier of build.
124
+ * @param requestInit The initializer for the request.
125
+ * @returns The build.
126
+ */
127
+ async cancel(appIdentity, buildIdentity, requestInit = {}) {
128
+ const response = await fetch(`${this.endpoint}/apps/${appIdentity}/builds/${buildIdentity}`, {
129
+ ...requestInit,
130
+ method: 'DELETE',
131
+ headers: {
132
+ ...requestInit?.headers,
133
+ Accept: 'application/vnd.heroku+json; version=3.sdk',
134
+ 'Content-Type': 'application/json'
135
+ }
136
+ });
137
+ if (response.ok) {
138
+ return (await response.json());
139
+ }
140
+ let message = response.statusText;
141
+ try {
142
+ ({ message } = (await response.json()));
143
+ }
144
+ catch {
145
+ // no-op
146
+ }
147
+ throw new Error(`${response.status}: ${message}`, { cause: response });
148
+ }
149
+ }
@@ -0,0 +1,42 @@
1
+ /**
2
+ * [Heroku Platform API - Source](https://devcenter.heroku.com/articles/platform-api-reference#source)
3
+ * A source is a location for uploading and downloading an application's source code.
4
+ */
5
+ export default class SourceService {
6
+ endpoint;
7
+ /**
8
+ *
9
+ * @param endpoint The endpoint to use for the source service.
10
+ */
11
+ constructor(endpoint) {
12
+ this.endpoint = endpoint;
13
+ }
14
+ /**
15
+ * Create URLs for uploading and downloading source.
16
+ *
17
+ * @param requestInit The initializer for the request.
18
+ * @returns The source.
19
+ */
20
+ async create(requestInit = {}) {
21
+ const response = await fetch(`${this.endpoint}/sources`, {
22
+ ...requestInit,
23
+ method: 'POST',
24
+ headers: {
25
+ ...requestInit?.headers,
26
+ Accept: 'application/vnd.heroku+json; version=3.sdk',
27
+ 'Content-Type': 'application/json'
28
+ }
29
+ });
30
+ if (response.ok) {
31
+ return (await response.json());
32
+ }
33
+ let message = response.statusText;
34
+ try {
35
+ ({ message } = (await response.json()));
36
+ }
37
+ catch {
38
+ // no-op
39
+ }
40
+ throw new Error(`${response.status}: ${message}`, { cause: response });
41
+ }
42
+ }