Skip to content

Commit

Permalink
Merge pull request #1 from RihanArfan/fix-cloudflare-capitalisation
Browse files Browse the repository at this point in the history
refactor!: ✏️ fix capitalisation of cloudflare
  • Loading branch information
allevo authored Nov 15, 2023
2 parents e06b82e + 9829e49 commit c590913
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 56 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ npm install @orama/cloudflare-api
## Usage

```ts
import { CloudFlareApi } from '@orama/cloudflare-api';
import { CloudflareApi } from '@orama/cloudflare-api';

const apiKey = 'your-api-key'
const api = new CloudFlareApi({ apiKey })
const api = new CloudflareApi({ apiKey })

// Worker KV
const ACCOUNT_ID = 'your-account-id'
Expand Down
16 changes: 8 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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 {
export interface CloudflareApiOption {
apiKey: string
url?: string
}

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

constructor (option: CloudFlareApiOption) {
constructor (option: CloudflareApiOption) {
this.config = {
apiKey: option.apiKey,
url: option.url ?? 'https://api.cloudflare.com/client/v4'
}
}

workerKv (accountId: string, namespaceId: string): CloudFlareWorkerKv {
return new CloudFlareWorkerKv({
workerKv (accountId: string, namespaceId: string): CloudflareWorkerKv {
return new CloudflareWorkerKv({
...this.config,
accountId,
namespaceId
Expand Down
2 changes: 1 addition & 1 deletion src/internal_types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

export interface CloudFlareApiConfig {
export interface CloudflareApiConfig {
apiKey: string
url: string
}
Expand Down
8 changes: 4 additions & 4 deletions src/workerKv.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CloudFlareApiConfig, Method } from './internal_types.js'
import { CloudflareApiConfig, Method } from './internal_types.js'
import { checkSuccess, createError, makeHttpRequest } from './utils.js'

export interface CloudFlareWorkerKvConfig extends CloudFlareApiConfig {
export interface CloudflareWorkerKvConfig extends CloudflareApiConfig {
accountId: string
namespaceId: string
}
Expand All @@ -16,8 +16,8 @@ type ReturnAsType<T> =
T extends 'response' ? Response :
never

export class CloudFlareWorkerKv {
constructor (private readonly config: CloudFlareWorkerKvConfig) {}
export class CloudflareWorkerKv {
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)
Expand Down
20 changes: 10 additions & 10 deletions tests/deleteKV.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import t from 'node:test'
import assert from 'node:assert'

import { CloudFlareApi } from '../src/index.js'
import { buildFakeCloudFlareServer } from './helper.js'
import { CloudflareApi } from '../src/index.js'
import { buildFakeCloudflareServer } from './helper.js'

const API_KEY = 'a-fake-api-key'
const ACCOUNT_ID = '123'
Expand All @@ -11,10 +11,10 @@ const KEY = '789'

await t.test('deleteKv', async (t) => {
await t.test('should delete kv', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('DELETE', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { success: true })

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await workerKv.deleteKv(KEY)
Expand All @@ -24,10 +24,10 @@ await t.test('deleteKv', async (t) => {
})

await t.test('should throw an error on success: false', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('DELETE', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { success: false })

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await assert.rejects(workerKv.deleteKv(KEY), err => {
Expand All @@ -39,10 +39,10 @@ await t.test('deleteKv', async (t) => {
})

await t.test('should throw an error on 500', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('DELETE', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 500, {})

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await assert.rejects(workerKv.deleteKv(KEY), err => {
Expand All @@ -54,10 +54,10 @@ await t.test('deleteKv', async (t) => {
})

await t.test('should not break strage error on not JSON', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('DELETE', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 500, '{')

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await assert.rejects(workerKv.deleteKv(KEY), err => {
Expand Down
36 changes: 18 additions & 18 deletions tests/getKv.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import t from 'node:test'
import assert from 'node:assert'

import { CloudFlareApi } from '../src/index.js'
import { buildFakeCloudFlareServer } from './helper.js'
import { CloudflareApi } from '../src/index.js'
import { buildFakeCloudflareServer } from './helper.js'

const API_KEY = 'a-fake-api-key'
const ACCOUNT_ID = '123'
Expand All @@ -11,10 +11,10 @@ const KEY = '789'

await t.test('getKv', async (t) => {
await t.test('should get kv as json', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('GET', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { foo: 'bar' })

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

const obj = await workerKv.getKv(KEY, 'json')
Expand All @@ -26,10 +26,10 @@ await t.test('getKv', async (t) => {
})

await t.test('should get kv as json', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('GET', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { foo: 'bar' })

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

const obj = await workerKv.getKv(KEY, 'text')
Expand All @@ -41,10 +41,10 @@ await t.test('getKv', async (t) => {
})

await t.test('should get kv as arrayBuffer', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('GET', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, Buffer.from([1, 2, 3]))

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

const obj = await workerKv.getKv(KEY, 'arrayBuffer')
Expand All @@ -56,10 +56,10 @@ await t.test('getKv', async (t) => {
})

await t.test('should get kv as blob', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('GET', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, Buffer.from([1, 2, 3]))

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

const obj = await workerKv.getKv(KEY, 'blob')
Expand All @@ -73,10 +73,10 @@ await t.test('getKv', async (t) => {
})

await t.test('should get kv as response', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('GET', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { foo: 'bar' })

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

const response = await workerKv.getKv(KEY, 'response')
Expand All @@ -89,10 +89,10 @@ await t.test('getKv', async (t) => {

/*
await t.test('should throw an error on success: false', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('DELETE', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { success: false })
const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)
await assert.rejects(workerKv.deleteKv(KEY), err => {
Expand All @@ -104,10 +104,10 @@ await t.test('getKv', async (t) => {
})
await t.test('should throw an error on 500', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('DELETE', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 500, {})
const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)
await assert.rejects(workerKv.deleteKv(KEY), err => {
Expand All @@ -119,10 +119,10 @@ await t.test('getKv', async (t) => {
})
await t.test('should not break strage error on not JSON', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('DELETE', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 500, '{')
const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)
await assert.rejects(workerKv.deleteKv(KEY), err => {
Expand Down
2 changes: 1 addition & 1 deletion tests/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ declare module 'fastify' {
}
}

export async function buildFakeCloudFlareServer (t: any, apiKey: string): Promise<Fastify.FastifyInstance> {
export async function buildFakeCloudflareServer (t: any, apiKey: string): Promise<Fastify.FastifyInstance> {
const server = Fastify({
logger: {
level: 'error'
Expand Down
24 changes: 12 additions & 12 deletions tests/uploadKv.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import t from 'node:test'
import assert from 'node:assert'

import { CloudFlareApi } from '../src/index.js'
import { buildFakeCloudFlareServer } from './helper.js'
import { CloudflareApi } from '../src/index.js'
import { buildFakeCloudflareServer } from './helper.js'

const API_KEY = 'a-fake-api-key'
const ACCOUNT_ID = '123'
Expand All @@ -13,10 +13,10 @@ const VALUE_BLOB = new Blob([Buffer.from([1, 2, 3])])

await t.test('uploadKv', async (t) => {
await t.test('should upload string kv', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('PUT', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { success: true })

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await workerKv.uploadKv(KEY, VALUE)
Expand All @@ -28,10 +28,10 @@ await t.test('uploadKv', async (t) => {
})

await t.test('should upload blob kv', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('PUT', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { success: true })

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await workerKv.uploadKv(KEY, VALUE_BLOB)
Expand All @@ -43,10 +43,10 @@ await t.test('uploadKv', async (t) => {
})

await t.test('should throw an error on success: false', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('PUT', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 200, { success: false })

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await assert.rejects(workerKv.uploadKv(KEY, 'value'), err => {
Expand All @@ -58,10 +58,10 @@ await t.test('uploadKv', async (t) => {
})

await t.test('should throw an error on 500', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('PUT', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 500, {})

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await assert.rejects(workerKv.uploadKv(KEY, 'value'), err => {
Expand All @@ -73,10 +73,10 @@ await t.test('uploadKv', async (t) => {
})

await t.test('should not break strage error on not JSON', async (t) => {
const fakeServer = await buildFakeCloudFlareServer(t, API_KEY)
const fakeServer = await buildFakeCloudflareServer(t, API_KEY)
fakeServer.expectInvocation('PUT', `/accounts/${ACCOUNT_ID}/storage/kv/namespaces/${NAMESPACE_ID}/values/${KEY}`, 500, '{')

const api = new CloudFlareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const api = new CloudflareApi({ apiKey: API_KEY, url: fakeServer.getBaseUrl() })
const workerKv = api.workerKv(ACCOUNT_ID, NAMESPACE_ID)

await assert.rejects(workerKv.uploadKv(KEY, 'value'), err => {
Expand Down

0 comments on commit c590913

Please sign in to comment.