* XXX: We use private api here, which may be changed on the cloudflare end... * https://github.com/cloudflare/wrangler2/blob/main/packages/wrangler/src/d1/list.tsx#L34
(onSuccess)
| 72 | * https://github.com/cloudflare/wrangler2/blob/main/packages/wrangler/src/d1/list.tsx#L34 |
| 73 | */ |
| 74 | getDatabaseId(onSuccess) { |
| 75 | const dbName = this.currentEnv !== 'development' ? this._non_dev_db() : 'FEED_DB'; |
| 76 | const accountId = this.v.get('CLOUDFLARE_ACCOUNT_ID'); |
| 77 | const apiKey = this.v.get('CLOUDFLARE_API_TOKEN'); |
| 78 | const options = { |
| 79 | host: 'api.cloudflare.com', |
| 80 | port: '443', |
| 81 | path: `/client/v4/accounts/${accountId}/d1/database?name=${dbName}`, |
| 82 | method: 'GET', |
| 83 | headers: { |
| 84 | 'Authorization': `Bearer ${apiKey}`, |
| 85 | }, |
| 86 | }; |
| 87 | const request = https.request(options, (response) => { |
| 88 | let data = ''; |
| 89 | response.on('data', (chunk) => { |
| 90 | data = data + chunk.toString(); |
| 91 | }); |
| 92 | |
| 93 | response.on('end', () => { |
| 94 | const body = JSON.parse(data); |
| 95 | let databaseId = ''; |
| 96 | body.result.forEach((result) => { |
| 97 | if (result.name === dbName) { |
| 98 | databaseId = result.uuid; |
| 99 | } |
| 100 | }); |
| 101 | onSuccess(databaseId); |
| 102 | }); |
| 103 | }) |
| 104 | |
| 105 | request.on('error', (error) => { |
| 106 | console.log('An error', error); |
| 107 | onSuccess(''); |
| 108 | }); |
| 109 | |
| 110 | request.end(); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | module.exports = { |
no test coverage detected