@adonis-agora/media
Version:
Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a
93 lines • 4.99 kB
TypeScript
import type { ImageProcessor } from '../image_processor.js';
import type { MediaLibrary } from '../media_library.js';
import type { MediaStore } from '../media_store.js';
import { type DiskVisibility, InMemoryDisk } from './in_memory_disk.js';
/**
* The {@link MediaLibrary} methods a {@link MediaFake} records and lets you {@link MediaFake.stub}.
* `for(...)` is not listed: its binding calls `attach`/`attachExisting`/`list` through the fake,
* so those are what get recorded.
*/
export declare const MEDIA_FAKE_METHODS: readonly ['attach', 'attachExisting', 'list', 'page', 'rename', 'find', 'delete', 'url', 'deliver', 'signedUrl', 'ensureConversion', 'transform'];
/** A {@link MediaLibrary} method name a {@link MediaFake} records. */
export type MediaFakeMethod = (typeof MEDIA_FAKE_METHODS)[number];
/** The arguments of one recorded call, typed per method (`signedUrl` → `[id, expiresIn, options?]`). */
export type MediaFakeCall<K extends MediaFakeMethod> = Parameters<MediaLibrary[K]>;
/** Every recorded call, per method, in call order. */
export type MediaFakeCalls = {
readonly [K in MediaFakeMethod]: MediaFakeCall<K>[];
};
/**
* A replacement for one library method. May return the value or a promise of it, and may throw —
* a synchronous throw still surfaces as a rejected promise, like the real method.
*/
export type MediaFakeStub<K extends MediaFakeMethod> = (...args: MediaFakeCall<K>) => Awaited<ReturnType<MediaLibrary[K]>> | ReturnType<MediaLibrary[K]>;
/** Options for `media.fake()`. */
export interface MediaFakeOptions {
/**
* Visibility every in-memory disk reports — drives `delivery.mode: 'auto'` (`private` → signed
* redirect, `public` → public redirect, `unknown` → proxy stream). Default `private`.
*/
visibility?: DiskVisibility;
/** Store backing the fake. Default: a fresh `InMemoryMediaStore`. */
store?: MediaStore;
/** Image processor for conversions. Default: a fresh `FakeImageProcessor` (no sharp). */
imageProcessor?: ImageProcessor;
}
/**
* In-memory disks created on demand for ANY name, so every disk a config or collection names
* resolves without listing them up front. `undefined` resolves to `defaultDisk`.
*/
export declare function lazyInMemoryDiskResolver(defaultDisk: string, visibility?: DiskVisibility): {
resolve: (name?: string) => InMemoryDisk;
disks: Record<string, InMemoryDisk>;
};
/**
* The handle `media.fake()` returns. While it is active, `media.library` (and `media.store`,
* `media.storage`, `media.attachments`, …) run on real library code over in-memory doubles, and
* every library call is recorded here.
*
* ```ts
* const fake = media.fake()
* await someCodeThatUploads()
* fake.assertCalled('attach', (input) => input.collection === 'avatar')
* assert.equal(fake.calls.signedUrl[0]?.[1], 300) // TTL
* fake.stub('deliver', async () => ({ kind: 'redirect', url: 'https://x.test' }))
* media.restore()
* ```
*
* Built by `MediaManager.fake()`; not meant to be constructed directly.
*/
export declare class MediaFake {
#private;
/** Recorded calls per method — the args tuple of each call, in order. Stubbed calls included. */
readonly calls: MediaFakeCalls;
/** The faked library — the very object `media.library` returns while this fake is active. */
readonly library: MediaLibrary;
/** The store backing the fake (an `InMemoryMediaStore` unless one was passed). */
readonly store: MediaStore;
/** In-memory disks created so far, by name (every name resolves; this lists the ones touched). */
readonly disks: Record<string, InMemoryDisk>;
constructor(input: {
library: MediaLibrary;
store: MediaStore;
resolve: (name?: string) => InMemoryDisk;
disks: Record<string, InMemoryDisk>;
onRestore: () => void;
});
/** The in-memory disk behind `name` (default disk when omitted) — inspect `.files` for bytes. */
disk(name?: string): InMemoryDisk;
/**
* Replace one library method for the rest of this fake (calls are still recorded). Use it to
* force a result or an error the in-memory library would not produce on its own.
*/
stub<K extends MediaFakeMethod>(method: K, implementation: MediaFakeStub<K>): this;
/** Drop a {@link stub}, so the method runs the real in-memory library again. */
unstub(method: MediaFakeMethod): this;
/** Throws unless `method` was called (and, with `predicate`, called with matching args). */
assertCalled<K extends MediaFakeMethod>(method: K, predicate?: (...args: MediaFakeCall<K>) => boolean): void;
/** Throws if `method` was called (or, with `predicate`, called with matching args). */
assertNotCalled<K extends MediaFakeMethod>(method: K, predicate?: (...args: MediaFakeCall<K>) => boolean): void;
/** Same as `media.restore()`, but a no-op once a newer fake has replaced this one. */
restore(): void;
}
//# sourceMappingURL=media_fake.d.ts.map