firebase-storage
Advanced tools
Comparing version 0.0.6 to 0.0.7
@@ -27,3 +27,3 @@ export interface StorageObject { | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
name: string; | ||
@@ -33,3 +33,3 @@ }) => Promise<StorageObject>; | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
name: string; | ||
@@ -39,3 +39,3 @@ }) => Promise<ArrayBuffer>; | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
name: string; | ||
@@ -50,3 +50,3 @@ file: Blob; | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
name: string; | ||
@@ -56,4 +56,7 @@ }) => Promise<boolean>; | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
}) => Promise<StorageObject[]>; | ||
type ParamType<T extends (...args: never[]) => unknown, P = Parameters<T>[0]> = Omit<P, "token" | "bucket"> & { | ||
bucket?: string; | ||
}; | ||
export declare const getStorage: ({ clientEmail, privateKey, bucket: _bucket, }: { | ||
@@ -64,7 +67,8 @@ clientEmail: string; | ||
}) => { | ||
info: (params: Omit<Parameters<typeof info>[0], "token">) => Promise<StorageObject>; | ||
download: (params: Omit<Parameters<typeof download>[0], "token">) => Promise<ArrayBuffer>; | ||
upload: (params: Omit<Parameters<typeof upload>[0], "token">) => Promise<any>; | ||
del: (params: Omit<Parameters<typeof del>[0], "token">) => Promise<boolean>; | ||
list: (params: Omit<Parameters<typeof list>[0], "token">) => Promise<StorageObject[]>; | ||
info: (params: ParamType<typeof info>) => Promise<StorageObject>; | ||
download: (params: ParamType<typeof download>) => Promise<ArrayBuffer>; | ||
upload: (params: ParamType<typeof upload>) => Promise<any>; | ||
del: (params: ParamType<typeof del>) => Promise<boolean>; | ||
list: (params: ParamType<typeof list>) => Promise<StorageObject[]>; | ||
}; | ||
export {}; |
@@ -92,19 +92,47 @@ "use strict"; | ||
const getStorage = ({ clientEmail, privateKey, bucket: _bucket, }) => { | ||
const token = (0, exports.createToken)({ clientEmail, privateKey }); | ||
const property = { | ||
token: "", | ||
expire: 0, | ||
}; | ||
const getToken = async () => { | ||
if (property.expire > Date.now() / 1000 + 300) | ||
return property.token; | ||
property.expire = Math.floor(Date.now() / 1000) + 3600; | ||
property.token = await (0, exports.createToken)({ clientEmail, privateKey }); | ||
return property.token; | ||
}; | ||
const getBucket = (bucket) => { | ||
const result = _bucket ?? bucket; | ||
if (!result) | ||
throw new Error("bucket is not defined"); | ||
return result; | ||
}; | ||
return { | ||
info: async (params) => (0, exports.info)({ ...params, token: await token, bucket: params.bucket ?? _bucket }), | ||
info: async (params) => (0, exports.info)({ | ||
...params, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
download: async (params) => (0, exports.download)({ | ||
...params, | ||
token: await token, | ||
bucket: params.bucket ?? _bucket, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
upload: async (params) => (0, exports.upload)({ | ||
...params, | ||
token: await token, | ||
bucket: params.bucket ?? _bucket, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
del: async (params) => (0, exports.del)({ ...params, token: await token, bucket: params.bucket ?? _bucket }), | ||
list: async (params) => (0, exports.list)({ ...params, token: await token, bucket: params.bucket ?? _bucket }), | ||
del: async (params) => (0, exports.del)({ | ||
...params, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
list: async (params) => (0, exports.list)({ | ||
...params, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
}; | ||
}; | ||
exports.getStorage = getStorage; |
@@ -27,3 +27,3 @@ export interface StorageObject { | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
name: string; | ||
@@ -33,3 +33,3 @@ }) => Promise<StorageObject>; | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
name: string; | ||
@@ -39,3 +39,3 @@ }) => Promise<ArrayBuffer>; | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
name: string; | ||
@@ -50,3 +50,3 @@ file: Blob; | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
name: string; | ||
@@ -56,4 +56,7 @@ }) => Promise<boolean>; | ||
token: string; | ||
bucket?: string | undefined; | ||
bucket: string; | ||
}) => Promise<StorageObject[]>; | ||
type ParamType<T extends (...args: never[]) => unknown, P = Parameters<T>[0]> = Omit<P, "token" | "bucket"> & { | ||
bucket?: string; | ||
}; | ||
export declare const getStorage: ({ clientEmail, privateKey, bucket: _bucket, }: { | ||
@@ -64,7 +67,8 @@ clientEmail: string; | ||
}) => { | ||
info: (params: Omit<Parameters<typeof info>[0], "token">) => Promise<StorageObject>; | ||
download: (params: Omit<Parameters<typeof download>[0], "token">) => Promise<ArrayBuffer>; | ||
upload: (params: Omit<Parameters<typeof upload>[0], "token">) => Promise<any>; | ||
del: (params: Omit<Parameters<typeof del>[0], "token">) => Promise<boolean>; | ||
list: (params: Omit<Parameters<typeof list>[0], "token">) => Promise<StorageObject[]>; | ||
info: (params: ParamType<typeof info>) => Promise<StorageObject>; | ||
download: (params: ParamType<typeof download>) => Promise<ArrayBuffer>; | ||
upload: (params: ParamType<typeof upload>) => Promise<any>; | ||
del: (params: ParamType<typeof del>) => Promise<boolean>; | ||
list: (params: ParamType<typeof list>) => Promise<StorageObject[]>; | ||
}; | ||
export {}; |
@@ -83,18 +83,46 @@ import { SignJWT, importPKCS8 } from "jose"; | ||
export const getStorage = ({ clientEmail, privateKey, bucket: _bucket, }) => { | ||
const token = createToken({ clientEmail, privateKey }); | ||
const property = { | ||
token: "", | ||
expire: 0, | ||
}; | ||
const getToken = async () => { | ||
if (property.expire > Date.now() / 1000 + 300) | ||
return property.token; | ||
property.expire = Math.floor(Date.now() / 1000) + 3600; | ||
property.token = await createToken({ clientEmail, privateKey }); | ||
return property.token; | ||
}; | ||
const getBucket = (bucket) => { | ||
const result = _bucket ?? bucket; | ||
if (!result) | ||
throw new Error("bucket is not defined"); | ||
return result; | ||
}; | ||
return { | ||
info: async (params) => info({ ...params, token: await token, bucket: params.bucket ?? _bucket }), | ||
info: async (params) => info({ | ||
...params, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
download: async (params) => download({ | ||
...params, | ||
token: await token, | ||
bucket: params.bucket ?? _bucket, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
upload: async (params) => upload({ | ||
...params, | ||
token: await token, | ||
bucket: params.bucket ?? _bucket, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
del: async (params) => del({ ...params, token: await token, bucket: params.bucket ?? _bucket }), | ||
list: async (params) => list({ ...params, token: await token, bucket: params.bucket ?? _bucket }), | ||
del: async (params) => del({ | ||
...params, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
list: async (params) => list({ | ||
...params, | ||
token: await getToken(), | ||
bucket: getBucket(params.bucket), | ||
}), | ||
}; | ||
}; |
{ | ||
"name": "firebase-storage", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"main": "./dist/cjs/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./dist/cjs/index.d.ts", |
# firebase-storage | ||
Easily operate Google cloud storage with a private key. | ||
Works with `Edge runtime`. | ||
@@ -10,8 +11,9 @@ ## Sample | ||
const privateKey = `-----BEGIN PRIVATE KEY-----\nXXXXXXXXXXXXXXXX-----END PRIVATE KEY-----\n`; | ||
const privateKey = | ||
"-----BEGIN PRIVATE KEY-----\nXXXXXXXXXXXXXXXX-----END PRIVATE KEY-----\n"; | ||
const clientEmail = | ||
"firebase-adminsdk-XXXXX0XXXXX@XXXXXXX.iam.gserviceaccount.com"; | ||
const bucket = "XXXXXXXX.appspot.com"; // Can also be specified in each function | ||
async function main() { | ||
const bucket = "XXXXXXXX.appspot.com"; | ||
const storage = getStorage({ privateKey, clientEmail, bucket }); | ||
@@ -18,0 +20,0 @@ const file = new Blob(["Test value"], { |
16398
400
47