Skip to content

Commit b8da6a8

Browse files
committed
feat: rename ttl parameter to expiration
1 parent eb635fd commit b8da6a8

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

src/main.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ describe('set', () => {
323323
expect(store.fulfilled).toBeTruthy()
324324
})
325325

326-
test('Accepts a TTL parameter', async () => {
327-
const ttl = new Date(Date.now() + 15_000)
326+
test('Accepts an `expiration` parameter', async () => {
327+
const expiration = new Date(Date.now() + 15_000)
328328
const store = new MockFetch()
329329
.put({
330330
headers: { authorization: `Bearer ${apiToken}` },
@@ -335,7 +335,7 @@ describe('set', () => {
335335
body: value,
336336
headers: {
337337
'cache-control': 'max-age=0, stale-while-revalidate=60',
338-
'x-nf-expires-at': ttl.getTime().toString(),
338+
'x-nf-expires-at': expiration.getTime().toString(),
339339
},
340340
response: new Response(null),
341341
url: signedURL,
@@ -349,7 +349,7 @@ describe('set', () => {
349349
siteID,
350350
})
351351

352-
await blobs.set(key, value, { ttl })
352+
await blobs.set(key, value, { expiration })
353353

354354
expect(store.fulfilled).toBeTruthy()
355355
})
@@ -694,8 +694,8 @@ describe('setJSON', () => {
694694
expect(store.fulfilled).toBeTruthy()
695695
})
696696

697-
test('Accepts a TTL parameter', async () => {
698-
const ttl = new Date(Date.now() + 15_000)
697+
test('Accepts an `expiration` parameter', async () => {
698+
const expiration = new Date(Date.now() + 15_000)
699699
const store = new MockFetch()
700700
.put({
701701
headers: { authorization: `Bearer ${apiToken}` },
@@ -706,7 +706,7 @@ describe('setJSON', () => {
706706
body: JSON.stringify({ value }),
707707
headers: {
708708
'cache-control': 'max-age=0, stale-while-revalidate=60',
709-
'x-nf-expires-at': ttl.getTime().toString(),
709+
'x-nf-expires-at': expiration.getTime().toString(),
710710
},
711711
response: new Response(null),
712712
url: signedURL,
@@ -720,7 +720,7 @@ describe('setJSON', () => {
720720
siteID,
721721
})
722722

723-
await blobs.setJSON(key, { value }, { ttl })
723+
await blobs.setJSON(key, { value }, { expiration })
724724

725725
expect(store.fulfilled).toBeTruthy()
726726
})

src/main.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ enum HTTPMethod {
3030
}
3131

3232
interface SetOptions {
33-
ttl?: Date | number
33+
expiration?: Date | number
3434
}
3535

3636
interface SetFilesItem extends SetOptions {
@@ -100,7 +100,7 @@ export class Blobs {
100100
}
101101
}
102102

103-
private static getTTLHeaders(ttl: Date | number | undefined): Record<string, string> {
103+
private static getExpirationeaders(ttl: Date | number | undefined): Record<string, string> {
104104
if (typeof ttl === 'number') {
105105
return {
106106
[EXPIRY_HEADER]: (Date.now() + ttl).toString(),
@@ -186,12 +186,12 @@ export class Blobs {
186186
): Promise<ArrayBuffer | Blob | ReadableStream | string | null> {
187187
const { type } = options ?? {}
188188
const res = await this.makeStoreRequest(key, HTTPMethod.Get)
189-
const expiry = res?.headers.get(EXPIRY_HEADER)
189+
const expiration = res?.headers.get(EXPIRY_HEADER)
190190

191-
if (typeof expiry === 'string') {
192-
const expiryTS = Number.parseInt(expiry)
191+
if (typeof expiration === 'string') {
192+
const expirationTS = Number.parseInt(expiration)
193193

194-
if (!Number.isNaN(expiryTS) && expiryTS <= Date.now()) {
194+
if (!Number.isNaN(expirationTS) && expirationTS <= Date.now()) {
195195
return null
196196
}
197197
}
@@ -223,17 +223,17 @@ export class Blobs {
223223
throw new Error(`Invalid 'type' property: ${type}. Expected: arrayBuffer, blob, json, stream, or text.`)
224224
}
225225

226-
async set(key: string, data: BlobInput, { ttl }: SetOptions = {}) {
227-
const headers = Blobs.getTTLHeaders(ttl)
226+
async set(key: string, data: BlobInput, { expiration }: SetOptions = {}) {
227+
const headers = Blobs.getExpirationeaders(expiration)
228228

229229
await this.makeStoreRequest(key, HTTPMethod.Put, headers, data)
230230
}
231231

232-
async setFile(key: string, path: string, { ttl }: SetOptions = {}) {
232+
async setFile(key: string, path: string, { expiration }: SetOptions = {}) {
233233
const { size } = await stat(path)
234234
const file = Readable.toWeb(createReadStream(path))
235235
const headers = {
236-
...Blobs.getTTLHeaders(ttl),
236+
...Blobs.getExpirationeaders(expiration),
237237
'content-length': size.toString(),
238238
}
239239

@@ -244,10 +244,10 @@ export class Blobs {
244244
return pMap(files, ({ key, path, ...options }) => this.setFile(key, path, options), { concurrency })
245245
}
246246

247-
async setJSON(key: string, data: unknown, { ttl }: SetOptions = {}) {
247+
async setJSON(key: string, data: unknown, { expiration }: SetOptions = {}) {
248248
const payload = JSON.stringify(data)
249249
const headers = {
250-
...Blobs.getTTLHeaders(ttl),
250+
...Blobs.getExpirationeaders(expiration),
251251
'content-type': 'application/json',
252252
}
253253

0 commit comments

Comments
 (0)