-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add worker and secret management to setup scripts
- Loading branch information
Showing
6 changed files
with
142 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Data } from "@edgefirst-dev/data"; | ||
import { ObjectParser } from "@edgefirst-dev/data/parser"; | ||
import type Cloudflare from "cloudflare"; | ||
import consola from "consola"; | ||
import { generatePath } from "react-router"; | ||
import type { Account } from "./account"; | ||
import { Worker } from "./worker"; | ||
|
||
export class Secret extends Data<ObjectParser> { | ||
get name() { | ||
return this.parser.string("name"); | ||
} | ||
|
||
get type() { | ||
return this.parser.string("type"); | ||
} | ||
|
||
static async create( | ||
cf: Cloudflare, | ||
account: Account, | ||
worker: Worker, | ||
name: string, | ||
text: string, | ||
) { | ||
consola.info(`Creating secret ${name}.`); | ||
|
||
let path = generatePath( | ||
"/client/v4/accounts/:accountId/workers/scripts/:name/secrets", | ||
{ accountId: account.id, name: worker.name }, | ||
); | ||
|
||
let response = await fetch(new URL(path, "https://api.cloudflare.com"), { | ||
method: "PUT", | ||
body: JSON.stringify({ name, text, type: "secret_text" }), | ||
headers: { | ||
Authorization: `Bearer ${cf.apiToken}`, | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
if (!response.ok) throw new Error(`Failed to create secret ${name}.`); | ||
|
||
let result = await response.json(); | ||
let parser = new ObjectParser(result); | ||
|
||
consola.success(`Created secret ${name}.`); | ||
|
||
return new Secret(parser.object("result")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Data } from "@edgefirst-dev/data"; | ||
import { ObjectParser } from "@edgefirst-dev/data/parser"; | ||
import type { Cloudflare } from "cloudflare"; | ||
import consola from "consola"; | ||
import { generatePath, resolvePath } from "react-router"; | ||
import type { Account } from "./account"; | ||
|
||
export class Worker extends Data<ObjectParser> { | ||
get name() { | ||
return this.parser.string("id"); | ||
} | ||
|
||
static async create(cf: Cloudflare, account: Account, name: string) { | ||
consola.info(`Creating worker ${name}.`); | ||
|
||
let formData = new FormData(); | ||
formData.set("account_id", account.id); | ||
formData.set("metadata", JSON.stringify({ main_module: "worker.ts" })); | ||
formData.set( | ||
"worker.ts", | ||
new File(["export default { fetch() {} }"], "worker.ts", { | ||
type: "application/javascript+module", | ||
}), | ||
); | ||
|
||
let path = generatePath( | ||
"/client/v4/accounts/:accountId/workers/services/:name/environments/staging", | ||
{ accountId: account.id, name }, | ||
); | ||
|
||
let response = await fetch(new URL(path, "https://api.cloudflare.com"), { | ||
method: "PUT", | ||
body: formData, | ||
headers: { Authorization: `Bearer ${cf.apiToken}` }, | ||
}); | ||
|
||
let result = await response.json(); | ||
|
||
let parser = new ObjectParser(result); | ||
|
||
let worker = new Worker(parser.object("result")); | ||
|
||
consola.success(`Created worker ${worker.name}.`); | ||
|
||
return worker; | ||
} | ||
|
||
static async find(cf: Cloudflare, account: Account, name: string) { | ||
consola.info(`Looking up for Worker named ${name}...`); | ||
|
||
let { result } = await cf.workers.scripts.list({ | ||
account_id: account.id, | ||
}); | ||
|
||
let workers = result | ||
.map((item) => new Worker(new ObjectParser(item))) | ||
.filter((worker) => worker.name === name); | ||
|
||
if (workers.length === 0) { | ||
consola.info(`No workers found named ${name}.`); | ||
return null; | ||
} | ||
|
||
if (workers.length > 1) { | ||
throw new Error(`Found more than one worker named ${name}.`); | ||
} | ||
|
||
return workers.at(0) ?? null; | ||
} | ||
|
||
static async upsert(cf: Cloudflare, account: Account, projectName: string) { | ||
let worker = await Worker.find(cf, account, projectName); | ||
|
||
if (worker) { | ||
consola.success(`Using found worker ${projectName}.`); | ||
return worker; | ||
} | ||
|
||
return await Worker.create(cf, account, projectName); | ||
} | ||
} |