class CloudflareDurableObjects extends Cloudflare {
_scriptUploadModuleFormData(accoutId, scriptName, formData) {
return this.ky
.put(`accounts/${accoutId}/workers/scripts/${scriptName}`, {
body: formData
})
.json();
}
async scriptUploadModule(accoutId, scriptName, metadata, files) {
const data = new FormData();
data.append(
"metadata",
new Blob([JSON.stringify(metadata)], { type: "application/json" }),
"metadata.json"
);
for (const [fileName, fileContent] of Object.entries(files)) {
const type = /\.mjs$/.test(fileName)
? "application/javascript+module"
: "application/octet-stream";
data.append("files", new Blob([fileContent], { type }), fileName);
}
return await this._scriptUploadModuleFormData(accoutId, scriptName, data);
}
durableObjectNamespaceList(accoutId) {
return this.ky
.get(`accounts/${accoutId}/workers/durable_objects/namespaces/`, {})
.json();
}
durableObjectNamespaceCreate(accoutId, scriptName, className) {
return this.ky
.post(`accounts/${accoutId}/workers/durable_objects/namespaces/`, {
json: {
name: `${scriptName}-${className}`,
script: scriptName,
class: className
}
})
.json();
}
}