@adonisjs/drive
Version:
A thin wrapper on top of Flydrive to work seamlessly with AdonisJS
103 lines (102 loc) • 3.37 kB
TypeScript
import type { ConfigProvider } from '@adonisjs/core/types';
import type { S3Driver } from 'flydrive/drivers/s3';
import type { FSDriver } from 'flydrive/drivers/fs';
import type { GCSDriver } from 'flydrive/drivers/gcs';
import type { S3DriverOptions } from 'flydrive/drivers/s3/types';
import type { GCSDriverOptions } from 'flydrive/drivers/gcs/types';
import type { DriverFactory, AdonisFSDriverOptions, ServiceConfigProvider, ServiceWithLocalServer, DriveManagerOptions } from './types.ts';
/**
* Helper to remap known drive services to factory functions
*/
type ResolvedConfig<Services extends Record<string, DriverFactory>> = {
config: {
default: keyof Services;
fakes: DriveManagerOptions<Services>['fakes'];
services: {
[K in keyof Services]: Services[K] extends ServiceConfigProvider<infer A> ? A : Services[K];
};
};
locallyServed: ServiceWithLocalServer[];
};
/**
* Helper function to define configuration for FlyDrive
*
* @param config - The drive configuration object
* @param config.default - The name of the default disk to use
* @param config.fakes - Configuration for fake disk used during testing
* @param config.services - Object defining all available disk services
*
* @example
* ```js
* const driveConfig = defineConfig({
* default: 'uploads',
* services: {
* uploads: services.fs({
* location: new URL('./uploads', import.meta.url),
* visibility: 'public',
* serveFiles: true,
* routeBasePath: '/uploads'
* }),
* s3: services.s3({
* credentials: {
* accessKeyId: env.get('S3_ACCESS_KEY_ID'),
* secretAccessKey: env.get('S3_SECRET_ACCESS_KEY')
* },
* region: env.get('S3_REGION'),
* bucket: env.get('S3_BUCKET')
* })
* }
* })
* ```
*/
export declare function defineConfig<Services extends Record<string, DriverFactory>>(config: {
default: keyof Services;
fakes?: DriveManagerOptions<Services>['fakes'];
services: {
[K in keyof Services]: ServiceConfigProvider<Services[K]> | Services[K];
};
}): ConfigProvider<ResolvedConfig<Services>>;
/**
* Config helpers to register file storage services within the
* config file.
*
* @example
* ```js
* // Using in drive config
* {
* services: {
* local: services.fs({
* location: './uploads',
* visibility: 'public',
* serveFiles: true,
* routeBasePath: '/uploads'
* }),
* cloud: services.s3({
* credentials: { accessKeyId: '...', secretAccessKey: '...' },
* region: 'us-east-1',
* bucket: 'my-bucket'
* })
* }
* }
* ```
*/
export declare const services: {
/**
* Configure the "fs" driver to store files on the
* local filesystem and serve files using the
* AdonisJS HTTP server
*/
fs: (config: AdonisFSDriverOptions) => ServiceConfigProvider<() => FSDriver>;
/**
* Configure the "s3" driver to store files inside
* a S3 bucket and serve files using S3 directly
* or a CDN.
*/
s3: (config: S3DriverOptions) => ServiceConfigProvider<() => S3Driver>;
/**
* Configure the "gcs" driver to store files inside
* a GCS bucket and serve files using GCS directly.
*/
gcs: (config: GCSDriverOptions) => ServiceConfigProvider<() => GCSDriver>;
};
export {};