Skip to content

Commit

Permalink
Lint
Browse files Browse the repository at this point in the history
  • Loading branch information
allevo committed Nov 8, 2023
1 parent f76acb4 commit 6831b9e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 34 deletions.
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { CloudFlareApiConfig } from "./internal_types.js"
import { CloudFlareWorkerKv } from "./workerKv.js"
import { CloudFlareApiConfig } from './internal_types.js'
import { CloudFlareWorkerKv } from './workerKv.js'

export interface CloudFlareApiOption {
apiKey: string
url?: string
}


export class CloudFlareApi {
private config: CloudFlareApiConfig
private readonly config: CloudFlareApiConfig

constructor (option: CloudFlareApiOption) {
this.config = {
Expand All @@ -17,7 +16,7 @@ export class CloudFlareApi {
}
}

workerKv(accountId: string, namespaceId: string): CloudFlareWorkerKv {
workerKv (accountId: string, namespaceId: string): CloudFlareWorkerKv {
return new CloudFlareWorkerKv({
...this.config,
accountId,
Expand Down
14 changes: 3 additions & 11 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Method } from "./internal_types.js"
import { Method } from './internal_types.js'

export function createError (message: string, cause?: Error): Error {
return new Error(message, {
Expand All @@ -7,20 +7,12 @@ export function createError (message: string, cause?: Error): Error {
}

export function checkSuccess (json: any, errorStr: string): void {
if (json && json.success !== true) {
if (json != null && json.success !== true) {
throw createError(`${errorStr}: ${JSON.stringify(json)}`)
}
}

export async function performPut (url: string, headers: Headers, body: FormData): Promise<Response> {
return makeHttpRequest(Method.PUT, url, headers, body)
}

export async function performDelete (url: string, headers: Headers): Promise<Response> {
return makeHttpRequest(Method.DELETE, url, headers, undefined)
}

export async function makeHttpRequest(method: Method, url: string, headers: Headers, requestBody: BodyInit | undefined): Promise<Response> {
export async function makeHttpRequest (method: Method, url: string, headers: Headers, requestBody: BodyInit | undefined): Promise<Response> {
const r = await fetch(
url,
{ method, headers, body: requestBody }
Expand Down
36 changes: 18 additions & 18 deletions src/workerKv.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CloudFlareApiConfig, Method } from "./internal_types.js";
import { checkSuccess, createError, makeHttpRequest, performDelete, performPut } from "./utils.js";
import { CloudFlareApiConfig, Method } from './internal_types.js'
import { checkSuccess, createError, makeHttpRequest } from './utils.js'

export interface CloudFlareWorkerKvConfig extends CloudFlareApiConfig {
accountId: string
Expand All @@ -8,29 +8,29 @@ export interface CloudFlareWorkerKvConfig extends CloudFlareApiConfig {

type ReturnAs = 'json' | 'blob' | 'arrayBuffer' | 'text' | 'response'

type ReturnAsType<T> =
T extends "json" ? unknown :
T extends "text" ? string :
T extends "blob" ? Blob :
T extends "arrayBuffer" ? ArrayBuffer :
T extends "response" ? Response :
never;
type ReturnAsType<T> =
T extends 'json' ? unknown :
T extends 'text' ? string :
T extends 'blob' ? Blob :
T extends 'arrayBuffer' ? ArrayBuffer :
T extends 'response' ? Response :
never

export class CloudFlareWorkerKv {
constructor(private config: CloudFlareWorkerKvConfig) {}
constructor (private readonly config: CloudFlareWorkerKvConfig) {}

async getKv<T extends ReturnAs>(key: string, returnAs: T): Promise<ReturnAsType<T>> {
const response = await this.perform(Method.GET, key, undefined)

switch (returnAs) {
case 'json':
return response.json()
return await response.json()
case 'blob':
return response.blob() as Promise<ReturnAsType<T>>
return await (response.blob() as Promise<ReturnAsType<T>>)
case 'arrayBuffer':
return response.arrayBuffer() as Promise<ReturnAsType<T>>
return await (response.arrayBuffer() as Promise<ReturnAsType<T>>)
case 'text':
return response.text() as Promise<ReturnAsType<T>>
return await (response.text() as Promise<ReturnAsType<T>>)
case 'response':
return response as ReturnAsType<T>
default:
Expand All @@ -44,17 +44,17 @@ export class CloudFlareWorkerKv {
formdata.append('metadata', '{}')

const response = await this.perform(Method.PUT, key, formdata)
const json = await response.json() as any
const json = await response.json()
checkSuccess(json, `failed to upload kv ${key}`)
}

async deleteKv (key: string): Promise<void> {
const response = await this.perform(Method.DELETE, key, undefined)
const json = await response.json() as any
const json = await response.json()
checkSuccess(json, `failed to delete kv ${key}`)
}

private async perform(method: Method, key: string, body: FormData | undefined): Promise<Response> {
private async perform (method: Method, key: string, body: FormData | undefined): Promise<Response> {
const url = `${this.config.url}/accounts/${this.config.accountId}/storage/kv/namespaces/${this.config.namespaceId}/values/${key}`
const headers = new Headers()
headers.append('Authorization', `Bearer ${this.config.apiKey}`)
Expand All @@ -68,4 +68,4 @@ export class CloudFlareWorkerKv {

return response
}
}
}

0 comments on commit 6831b9e

Please sign in to comment.