@jrmc/adonis-attachment 2.4.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/index.d.ts CHANGED
@@ -6,3 +6,4 @@ export { defineConfig } from './src/define_config.js';
6
6
  export { Attachmentable } from './src/mixins/attachmentable.js';
7
7
  export * as errors from './src/errors.js';
8
8
  export { attachmentManager };
9
+ export { type AttachmentVariants } from './src/types/config.js';
@@ -5,10 +5,10 @@
5
5
  * @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
6
6
  */
7
7
  import type { ApplicationService } from '@adonisjs/core/types';
8
- import type { AttachmentManager } from '../src/attachment_manager.js';
8
+ import { AttachmentService } from '../src/types/config.js';
9
9
  declare module '@adonisjs/core/types' {
10
10
  interface ContainerBindings {
11
- 'jrmc.attachment': AttachmentManager;
11
+ 'jrmc.attachment': AttachmentService;
12
12
  }
13
13
  }
14
14
  export default class AttachmentProvider {
@@ -4,6 +4,6 @@
4
4
  * @license MIT
5
5
  * @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
6
6
  */
7
- import { AttachmentManager } from '../src/attachment_manager.js';
8
- declare let manager: AttachmentManager;
7
+ import { AttachmentService } from '../src/types/config.js';
8
+ declare let manager: AttachmentService;
9
9
  export { manager as default };
@@ -7,14 +7,14 @@
7
7
  import type { DriveService, SignedURLOptions } from '@adonisjs/drive/types';
8
8
  import type { MultipartFile } from '@adonisjs/core/bodyparser';
9
9
  import type { AttachmentBase, Attachment as AttachmentType } from './types/attachment.js';
10
- import type { ResolvedAttachmentConfig } from './types/config.js';
11
10
  import { DeferQueue } from '@poppinss/defer';
12
11
  import Converter from './converters/converter.js';
13
- export declare class AttachmentManager {
12
+ import { ResolvedAttachmentConfig } from './define_config.js';
13
+ export declare class AttachmentManager<KnownConverters extends Record<string, Converter>> {
14
14
  #private;
15
15
  queue: DeferQueue;
16
- constructor(config: ResolvedAttachmentConfig, drive: DriveService);
17
- getConfig(): ResolvedAttachmentConfig;
16
+ constructor(config: ResolvedAttachmentConfig<KnownConverters>, drive: DriveService);
17
+ getConfig(): ResolvedAttachmentConfig<KnownConverters>;
18
18
  createFromDbResponse(response: any): AttachmentType | null;
19
19
  createFromFile(file: MultipartFile): Promise<AttachmentType>;
20
20
  createFromBuffer(buffer: Buffer, name?: string): Promise<AttachmentType>;
@@ -67,11 +67,7 @@ export class AttachmentManager {
67
67
  }
68
68
  async getConverter(key) {
69
69
  if (this.#config.converters) {
70
- for (const c of this.#config.converters) {
71
- if (c.key === key) {
72
- return c.converter;
73
- }
74
- }
70
+ return this.#config.converters[key];
75
71
  }
76
72
  }
77
73
  async computeUrl(attachment, signedUrlOptions) {
@@ -4,6 +4,20 @@
4
4
  * @license MIT
5
5
  * @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
6
6
  */
7
- import type { AttachmentConfig, ResolvedAttachmentConfig } from './types/config.js';
7
+ import type { AttachmentConfig, BinPaths, ConverterConfig, Queue } from './types/config.js';
8
8
  import { ConfigProvider } from '@adonisjs/core/types';
9
- export declare function defineConfig(config: AttachmentConfig): ConfigProvider<ResolvedAttachmentConfig>;
9
+ import { Converter } from './types/converter.js';
10
+ /**
11
+ * Config resolved by the "defineConfig" method
12
+ */
13
+ export type ResolvedAttachmentConfig<KnownConverters extends Record<string, Converter>> = {
14
+ bin?: BinPaths;
15
+ meta?: boolean;
16
+ rename?: boolean;
17
+ preComputeUrl?: boolean;
18
+ converters?: {
19
+ [K in keyof KnownConverters]: KnownConverters[K];
20
+ };
21
+ queue?: Queue;
22
+ };
23
+ export declare function defineConfig<KnownConverter extends Record<string, ConverterConfig>>(config: AttachmentConfig<KnownConverter>): ConfigProvider<ResolvedAttachmentConfig<KnownConverter>>;
@@ -8,21 +8,20 @@ import { configProvider } from '@adonisjs/core';
8
8
  // export function defineConfig<T extends AttachmentConfig>(config: T): T {
9
9
  export function defineConfig(config) {
10
10
  return configProvider.create(async (_app) => {
11
- let convertersMap = [];
11
+ const convertersList = Object.keys(config.converters || {});
12
+ const converters = {};
12
13
  if (config.converters) {
13
- convertersMap = await Promise.all(config.converters.map(async (c) => {
14
+ for (let converterName of convertersList) {
15
+ const converter = config.converters[converterName];
14
16
  const binConfig = config.bin;
15
- const { default: value } = await c.converter();
17
+ const { default: value } = await converter.converter();
16
18
  const Converter = value;
17
- return {
18
- key: c.key,
19
- converter: new Converter(c.options, binConfig),
20
- };
21
- }));
19
+ converters[converterName] = new Converter(converter.options, binConfig);
20
+ }
22
21
  }
23
22
  return {
24
23
  ...config,
25
- converters: convertersMap,
24
+ converters,
26
25
  };
27
26
  });
28
27
  }
@@ -8,6 +8,7 @@ import type { DriveService } from '@adonisjs/drive/types';
8
8
  import type { Exif, Input } from './input.js';
9
9
  import type { Disk } from '@adonisjs/drive';
10
10
  import type { SignedURLOptions } from '@adonisjs/drive/types';
11
+ import type { AttachmentVariants } from '@jrmc/adonis-attachment';
11
12
  export type AttachmentBase = {
12
13
  drive: DriveService;
13
14
  input?: Input;
@@ -46,7 +47,7 @@ export type LucidOptions = {
46
47
  disk?: string;
47
48
  folder?: string;
48
49
  preComputeUrl?: boolean;
49
- variants?: string[];
50
+ variants?: (keyof AttachmentVariants)[];
50
51
  rename?: boolean;
51
52
  meta?: boolean;
52
53
  };
@@ -4,42 +4,45 @@
4
4
  * @license MIT
5
5
  * @copyright Jeremy Chaufourier <jeremy@chaufourier.fr>
6
6
  */
7
+ import { ConfigProvider } from '@adonisjs/core/types';
7
8
  import type { Converter, ConverterOptions } from './converter.js';
9
+ import { AttachmentManager } from '../attachment_manager.js';
8
10
  type ImportConverter = {
9
11
  default: unknown;
10
12
  };
11
- type ConverterConfig = {
12
- key: string;
13
+ export interface ConverterConfig {
13
14
  converter: () => Promise<ImportConverter>;
14
15
  options?: ConverterOptions;
15
- };
16
- type Queue = {
16
+ }
17
+ export interface Queue {
17
18
  concurrency: number;
18
- };
19
+ }
19
20
  export type BinPaths = {
20
21
  ffmpegPath?: string;
21
22
  ffprobePath?: string;
22
23
  pdftocairoBasePath?: string;
23
24
  libreofficePaths?: Array<string>;
24
25
  };
25
- export type AttachmentConfig = {
26
- bin?: BinPaths;
27
- meta?: boolean;
28
- rename?: boolean;
29
- preComputeUrl?: boolean;
30
- converters?: ConverterConfig[];
31
- queue?: Queue;
32
- };
33
- export type ResolvedConverter = {
34
- key: string;
35
- converter: Converter;
36
- };
37
- export type ResolvedAttachmentConfig = {
26
+ export type AttachmentConfig<KnownConverter extends Record<string, ConverterConfig>> = {
38
27
  bin?: BinPaths;
39
28
  meta?: boolean;
40
29
  rename?: boolean;
41
30
  preComputeUrl?: boolean;
42
- converters?: ResolvedConverter[];
31
+ converters?: {
32
+ [K in keyof KnownConverter]: KnownConverter[K];
33
+ };
43
34
  queue?: Queue;
44
35
  };
36
+ export interface AttachmentVariants {
37
+ }
38
+ export type InferConverters<Config extends ConfigProvider<{
39
+ bin?: unknown;
40
+ meta?: unknown;
41
+ rename?: unknown;
42
+ preComputeUrl?: unknown;
43
+ converters?: unknown;
44
+ queue?: unknown;
45
+ }>> = Exclude<Awaited<ReturnType<Config['resolver']>>['converters'], undefined>;
46
+ export interface AttachmentService extends AttachmentManager<AttachmentVariants extends Record<string, Converter> ? AttachmentVariants : never> {
47
+ }
45
48
  export {};
@@ -0,0 +1,5 @@
1
+ export * from './attachment.js';
2
+ export * from './config.js';
3
+ export * from './converter.js';
4
+ export * from './input.js';
5
+ export * from './mixin.js';
@@ -0,0 +1,5 @@
1
+ export * from './attachment.js';
2
+ export * from './config.js';
3
+ export * from './converter.js';
4
+ export * from './input.js';
5
+ export * from './mixin.js';
@@ -2,15 +2,21 @@
2
2
  exports({ to: app.configPath('attachment.ts') })
3
3
  }}}
4
4
  import { defineConfig } from '@jrmc/adonis-attachment'
5
+ import { InferConverters } from '@jrmc/adonis-attachment/types/config'
5
6
 
6
- export default defineConfig({
7
- converters: [
8
- {
9
- key: 'thumbnail',
7
+ const attachmentConfig = defineConfig({
8
+ converters: {
9
+ thumbnail: {
10
10
  converter: () => import('@jrmc/adonis-attachment/converters/image_converter'),
11
11
  options: {
12
12
  resize: 300,
13
13
  }
14
14
  }
15
- ]
15
+ }
16
16
  })
17
+
18
+ export default attachmentConfig
19
+
20
+ declare module '@jrmc/adonis-attachment' {
21
+ interface AttachmentVariants extends InferConverters<typeof attachmentConfig> {}
22
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jrmc/adonis-attachment",
3
- "version": "2.4.2",
3
+ "version": "3.0.0",
4
4
  "type": "module",
5
5
  "description": "Turn any field on your Lucid model to an attachment data type",
6
6
  "engines": {