diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f5978e1..53e32e9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,7 @@ permissions: env: NODE_ENV: production - JSR_DEPENDENCIES: "@cross/test @std/assert @std/fmt @frytg/logger @frytg/check-required-env" + JSR_DEPENDENCIES: "@cross/test @std/assert @std/fmt @frytg/logger" # NPM_DEPENDENCIES: "luxon minio sinon" jobs: diff --git a/storage-s3/s3.test.ts b/storage-s3/s3.test.ts index 57920af..a7d8108 100644 --- a/storage-s3/s3.test.ts +++ b/storage-s3/s3.test.ts @@ -9,9 +9,9 @@ import sinon from 'sinon' test('s3 - exports a Minio.Client instance', async () => { // Setup const envStub = sinon.stub(process, 'env').value({ - S3_ENDPOINT: 'test-endpoint', - MY_SCW_ACCESS_KEY: 'test-access-key', - MY_SCW_SECRET_KEY: 'test-secret-key', + STORE_S3_ENDPOINT: 'test-endpoint', + STORE_S3_ACCESS_KEY: 'test-access-key', + STORE_S3_SECRET_KEY: 'test-secret-key', }) // load module with stubbed env diff --git a/storage-s3/s3.ts b/storage-s3/s3.ts index 3bf9dd6..a588144 100644 --- a/storage-s3/s3.ts +++ b/storage-s3/s3.ts @@ -1,12 +1,17 @@ // load packages -import { getRequiredEnv } from '@frytg/check-required-env/get' +import process from 'node:process' // @deno-types="minio/dist/esm/minio.d.mts" import { Client } from 'minio' +// check environment variables +if (!process.env.STORE_S3_ENDPOINT) throw new Error('Environment variable STORE_S3_ENDPOINT is not defined') +if (!process.env.STORE_S3_ACCESS_KEY) throw new Error('Environment variable STORE_S3_ACCESS_KEY is not defined') +if (!process.env.STORE_S3_SECRET_KEY) throw new Error('Environment variable STORE_S3_SECRET_KEY is not defined') + // create a minio client export const minioClient = new Client({ useSSL: true, - endPoint: getRequiredEnv('S3_ENDPOINT', false), - accessKey: getRequiredEnv('MY_SCW_ACCESS_KEY', false), - secretKey: getRequiredEnv('MY_SCW_SECRET_KEY', false), + endPoint: process.env.STORE_S3_ENDPOINT, + accessKey: process.env.STORE_S3_ACCESS_KEY, + secretKey: process.env.STORE_S3_SECRET_KEY, }) diff --git a/storage-s3/storage.ts b/storage-s3/storage.ts index 0bb90a0..94f61a5 100644 --- a/storage-s3/storage.ts +++ b/storage-s3/storage.ts @@ -5,7 +5,6 @@ // import packages import { Buffer } from 'node:buffer' -import { getRequiredEnv } from '@frytg/check-required-env/get' import type { BucketItem, BucketItemStat, BucketStream, Client } from 'minio/dist/esm/minio.d.mts' // load utils @@ -19,33 +18,33 @@ export const client: Client = minioClient /** * Retrieves an object from S3. + * @param {string} bucketName - The name of the bucket to retrieve the object from. * @param {string} path - The path to the object in S3. * @param {object} options - The options for the operation. * @param {boolean} options.parseJson - Whether to parse the object as JSON. Defaults to `false`. * @param {boolean} options.throwError - Whether to throw an error if the object does not exist. Defaults to `true`. - * @param {string} options.bucketName - The name of the bucket to retrieve the object from. Defaults to env `S3_BUCKET_NAME`. * @returns {Promise} * * @see https://min.io/docs/minio/linux/developers/javascript/API.html#getObject * * @example * ```ts + * import { getRequiredEnv } from '@frytg/check-required-env/get' * import { getObject } from '@frytg/storage-s3' * - * const object = await getObject('path/to/object.json', { parseJson: true }) + * const object = await getObject(getRequiredEnv('S3_BUCKET_NAME'), 'path/to/object.json', { parseJson: true }) * console.log(object) * ``` */ export const getObject = ( + bucketName: string, path: string, { parseJson = false, throwError = true, - bucketName = getRequiredEnv('S3_BUCKET_NAME'), }: { parseJson?: boolean throwError?: boolean - bucketName?: string }, ): Promise => new Promise((resolve, reject) => { @@ -79,27 +78,21 @@ export const getObject = ( * * @example JSON * ```ts + * import { getRequiredEnv } from '@frytg/check-required-env/get' * import { uploadObject } from '@frytg/storage-s3' * - * await uploadObject('path/to/object.json', { foo: 'bar' }) + * await uploadObject(getRequiredEnv('S3_BUCKET_NAME'), 'path/to/object.json', { foo: 'bar' }) * ``` * * @example Buffer * ```ts + * import { getRequiredEnv } from '@frytg/check-required-env/get' * import { uploadObject } from '@frytg/storage-s3' * - * await uploadObject('path/to/object.blob', Buffer.from('foo')) + * await uploadObject(getRequiredEnv('S3_BUCKET_NAME'), 'path/to/object.blob', Buffer.from('foo')) * ``` */ -export const uploadObject = async ( - path: string, - data: Buffer | string, - { - bucketName = getRequiredEnv('S3_BUCKET_NAME'), - }: { - bucketName?: string - }, -): Promise => { +export const uploadObject = async (bucketName: string, path: string, data: Buffer | string): Promise => { // convert data to string if it's an object or array let dataString = data if (typeof data === 'object' || Array.isArray(data)) { @@ -121,20 +114,14 @@ export const uploadObject = async ( * * @example * ```ts + * import { getRequiredEnv } from '@frytg/check-required-env/get' * import { objectExists } from '@frytg/storage-s3' * - * const exists = await objectExists('path/to/object.json') + * const exists = await objectExists(getRequiredEnv('S3_BUCKET_NAME'), 'path/to/object.json') * console.log(exists) // null if it doesn't exist, otherwise the object stat * ``` */ -export const objectExists = async ( - path: string, - { - bucketName = getRequiredEnv('S3_BUCKET_NAME'), - }: { - bucketName?: string - }, -): Promise> => { +export const objectExists = async (bucketName: string, path: string): Promise> => { try { // explicitly awaiting the result to avoid unhandled promise rejection const result: BucketItemStat = await minioClient.statObject(bucketName, path) @@ -165,28 +152,29 @@ const readableStreamForListObjects = (stream: BucketStream): Promise * * @example * ```ts + * import { getRequiredEnv } from '@frytg/check-required-env/get' * import { listObjects } from '@frytg/storage-s3' * - * const objects = await listObjects('path/to/prefix') + * const objects = await listObjects(getRequiredEnv('S3_BUCKET_NAME'), 'path/to/prefix') * console.log(objects) * ``` * * @example Recursive * ```ts + * import { getRequiredEnv } from '@frytg/check-required-env/get' * import { listObjects } from '@frytg/storage-s3' * - * const objects = await listObjects('path/to/prefix', { recursive: true }) + * const objects = await listObjects(getRequiredEnv('S3_BUCKET_NAME'), 'path/to/prefix', { recursive: true }) * console.log(objects) * ``` */ export const listObjects = async ( + bucketName: string, prefix: string, { recursive = false, - bucketName = getRequiredEnv('S3_BUCKET_NAME'), }: { recursive?: boolean - bucketName?: string }, ): Promise => { const result: BucketStream = await minioClient.listObjectsV2(bucketName, prefix, recursive)