Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: made apiUrl optional and added tests #1

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ echo "accessKey:accessSecret" | base64

and save it to a `.env` file or somewhere else.

The default `apiUrl` is `s3.filebase.com`. To change, pass in the url to the `apiUrl` arg.

### Node.js

```ts
Expand All @@ -35,7 +37,6 @@ const url = await createPresignedUrl({
bucketName: `example-${crypto.randomUUID()}`,
token: process.env.FILEBASE_TOKEN,
file,
apiUrl: 's3.filebase.com',
})

await fetch(decodeURIComponent(url), { method: 'PUT', body: file })
Expand All @@ -61,7 +62,6 @@ const url = await createPresignedUrl({
bucketName: `example-${crypto.randomUUID()}`,
token: env.FILEBASE_TOKEN,
file,
apiUrl: 's3.filebase.com',
})

await fetch(decodeURIComponent(url), { method: 'PUT', body: file })
Expand All @@ -77,7 +77,7 @@ deno --allow-read --allow-net mod.ts

### createPresignedUrl

Creates a presigned URL for file upload. All options are required.
Creates a presigned URL for file upload. All options except apiUrl are required.

### getObject

Expand All @@ -91,7 +91,6 @@ import { getObject } from 'https://deno.land/x/filebase_upload/mod.ts'
const res = await getObject({
bucketName: `example-${crypto.randomUUID()}`,
token: env.FILEBASE_TOKEN,
apiUrl: 's3.filebase.com',
filename: 'hello.txt',
})

Expand All @@ -108,7 +107,6 @@ import { headObject } from 'https://deno.land/x/filebase_upload/mod.ts'
const [isUploaded, cid] = await headObject({
bucketName: `example-${crypto.randomUUID()}`,
token: env.FILEBASE_TOKEN,
apiUrl: 's3.filebase.com',
filename: 'hello.txt',
})

Expand Down
1 change: 1 addition & 0 deletions constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const FILEBASE_API_URL = 's3.filebase.com'
2 changes: 2 additions & 0 deletions dev_deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts'
export { assertEquals, assertThrows, assertStringIncludes } from 'https://deno.land/[email protected]/assert/mod.ts'
3 changes: 1 addition & 2 deletions example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ const file = new File(['Hello world'], 'hello.txt')

const init = {
bucketName: `filebase-upload-tests`,
token: env.FILEBASE_TOKEN,
apiUrl: 's3.filebase.com',
token: env.FILEBASE_TOKEN
}

const url = await createPresignedUrl({ ...init, file })
Expand Down
9 changes: 5 additions & 4 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FILEBASE_API_URL } from './constants.ts'
import {
aws4,
ChecksumConstructor,
Expand Down Expand Up @@ -79,10 +80,10 @@ class HttpRequest {
}

export const createPresignedUrl = async (
{ bucketName, apiUrl, file, token }: { bucketName: string; apiUrl: string; file: File; token: string },
{ bucketName, apiUrl, file, token }: { bucketName: string; apiUrl?: string; file: File; token: string },
) => {
await createBucket({ bucketName, apiUrl, token })
const url = parseUrl(`https://${apiUrl}/${bucketName}/${file.name}`)
const url = parseUrl(`https://${apiUrl ?? FILEBASE_API_URL}/${bucketName}/${file.name}`)
const presigner = new S3RequestPresigner({
credentials: fromEnv(token),
region: 'us-east-1',
Expand All @@ -102,7 +103,7 @@ export const headObject = async (
},
): Promise<[boolean, string | null]> => {
let requestOptions: aws4.Request & { key?: string } = {
host: `${bucketName}.${apiUrl}`,
host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
path: `/${filename}`,
key: `/${filename}`,
region: 'us-east-1',
Expand All @@ -127,7 +128,7 @@ export const getObject = async (
},
) => {
let requestOptions: aws4.Request & { key?: string } = {
host: `${bucketName}.${apiUrl}`,
host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
path: `/${filename}`,
key: `/${filename}`,
region: 'us-east-1',
Expand Down
21 changes: 17 additions & 4 deletions mod_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts'
import { assertEquals } from 'https://deno.land/[email protected]/assert/mod.ts'
import { assertEquals, assertStringIncludes, describe, it } from './dev_deps.ts'
import { getObject } from './mod.ts'

describe('getObject', () => {
Expand All @@ -8,9 +7,23 @@ describe('getObject', () => {
bucketName: 'filebase-upload-tests',
token: Deno.env.get('FILEBASE_TOKEN')!,
filename: 'hello.txt',
apiUrl: 's3.filebase.com',
})
assertEquals(await res.text(), 'Hello world')
assertEquals(res.headers.get('x-amz-meta-cid'), 'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve')
assertEquals(
res.headers.get('x-amz-meta-cid'),
'QmNRCQWfgze6AbBCaT1rkrkV5tJ2aP4oTNPb5JZcXYywve',
)
})
it('should accept custom `apiUrl`', async () => {
try {
await getObject({
bucketName: 'filebase-upload-tests',
token: Deno.env.get('FILEBASE_TOKEN')!,
filename: 'hello.txt',
apiUrl: 'localhost:5000',
})
} catch (error) {
assertStringIncludes(error.message, 'https://filebase-upload-tests.localhost:5000/hello.txt')
}
})
})
2 changes: 1 addition & 1 deletion types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type RequiredArgs = {
bucketName: string
apiUrl: string
apiUrl?: string
token: string
}
3 changes: 2 additions & 1 deletion utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { FILEBASE_API_URL } from './constants.ts'
import { aws4, AwsCredentialIdentity, Buffer, IHttpRequest, QueryParameterBag } from './deps.ts'
import { RequiredArgs } from './types.ts'

Expand Down Expand Up @@ -143,7 +144,7 @@ export const createBucket = async (
{ bucketName, apiUrl, token }: RequiredArgs,
) => {
let requestOptions: aws4.Request = {
host: `${bucketName}.${apiUrl}`,
host: `${bucketName}.${apiUrl ?? FILEBASE_API_URL}`,
region: 'us-east-1',
method: 'PUT',
service: 's3',
Expand Down
3 changes: 1 addition & 2 deletions utils_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { describe, it } from 'https://deno.land/[email protected]/testing/bdd.ts'
import { assertEquals, assertThrows } from 'https://deno.land/[email protected]/assert/mod.ts'
import { assertEquals, assertThrows, describe, it } from './dev_deps.ts'
import { parseUrl, toUint8Array } from './utils.ts'

describe('parseUrl', () => {
Expand Down