@asterflow/plugin 1.0.4 → 1.0.5

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/README.md CHANGED
@@ -113,7 +113,7 @@ const loggerPlugin = Plugin.create({ name: 'logger-plugin' })
113
113
  To use the created plugins, you register them with the `AsterFlow` instance.
114
114
 
115
115
  ```typescript
116
- import { AsterFlow } from '@asterflow/core'
116
+ import { AsterFlow } from 'asterflow'
117
117
  import { adapters } from '@asterflow/adapter'
118
118
  import fastify from 'fastify'
119
119
  // Import your plugins here
@@ -140,7 +140,7 @@ app.listen(server, { port: 3000 }, (err) => {
140
140
 
141
141
  ## 🔗 Related Packages
142
142
 
143
- - [@asterflow/core](https://www.npmjs.com/package/@asterflow/core) - The heart of the AsterFlow framework.
143
+ - [asterflow](https://www.npmjs.com/package/asterflow) - The heart of the AsterFlow framework.
144
144
  - [@asterflow/adapter](https://www.npmjs.com/package/@asterflow/adapter) - HTTP adapters for different runtimes.
145
145
  - [@asterflow/router](https://www.npmjs.com/package/@asterflow/router) - Type-safe routing system.
146
146
  - [@asterflow/request](https://www.npmjs.com/package/@asterflow/request) - Unified HTTP request system.
@@ -1,97 +1,71 @@
1
1
  "use strict";
2
2
  var r = Object.defineProperty;
3
- var h = Object.getOwnPropertyDescriptor;
4
- var d = Object.getOwnPropertyNames;
5
- var c = Object.prototype.hasOwnProperty;
6
- var f = (t, e) => {
3
+ var f = Object.getOwnPropertyDescriptor;
4
+ var u = Object.getOwnPropertyNames;
5
+ var d = Object.prototype.hasOwnProperty;
6
+ var g = (o, e) => {
7
7
  for (var n in e)
8
- r(t, n, { get: e[n], enumerable: !0 });
9
- }, u = (t, e, n, s) => {
8
+ r(o, n, { get: e[n], enumerable: !0 });
9
+ }, h = (o, e, n, t) => {
10
10
  if (e && typeof e == "object" || typeof e == "function")
11
- for (let o of d(e))
12
- !c.call(t, o) && o !== n && r(t, o, { get: () => e[o], enumerable: !(s = h(e, o)) || s.enumerable });
13
- return t;
11
+ for (let s of u(e))
12
+ !d.call(o, s) && s !== n && r(o, s, { get: () => e[s], enumerable: !(t = f(e, s)) || t.enumerable });
13
+ return o;
14
14
  };
15
- var g = (t) => u(r({}, "__esModule", { value: !0 }), t);
15
+ var x = (o) => h(r({}, "__esModule", { value: !0 }), o);
16
16
  // packages/plugin/src/index.ts
17
- var C = {};
18
- f(C, {
17
+ var y = {};
18
+ g(y, {
19
19
  Plugin: () => a
20
20
  });
21
- module.exports = g(C);
21
+ module.exports = x(y);
22
22
  // packages/plugin/src/controllers/Plugin.ts
23
- var a = class t {
23
+ var a = class o {
24
24
  name;
25
25
  resolvers;
26
26
  defaultConfig;
27
- hooks;
27
+ hooks = {};
28
28
  instance;
29
29
  _extensionFn;
30
- constructor(e, n, s, o, i) {
31
- this.name = e, this.resolvers = n, this.hooks = s, this.defaultConfig = o, this._extensionFn = i;
30
+ constructor(e, n, t, s, i) {
31
+ this.name = e, this.resolvers = n, this.hooks = t, this.defaultConfig = s, this._extensionFn = i;
32
32
  }
33
33
  config(e) {
34
- return new t(
35
- this.name,
36
- this.resolvers,
37
- this.hooks,
38
- e,
39
- this._extensionFn
40
- );
34
+ return this.defaultConfig = {
35
+ ...this.defaultConfig,
36
+ defaultConfig: e
37
+ }, this;
41
38
  }
42
39
  defineInstance(e) {
43
40
  return this.instance = e, this;
44
41
  }
45
42
  decorate(e, n) {
46
- let s = async (o, i) => ({
43
+ let t = async (s, i) => ({
47
44
  ...i,
48
45
  [e]: n
49
46
  });
50
- return new t(
51
- this.name,
52
- [...this.resolvers, s],
53
- this.hooks,
54
- this.defaultConfig,
55
- this._extensionFn
56
- );
47
+ return this.resolvers = [...this.resolvers, t], this;
57
48
  }
58
49
  derive(e, n) {
59
- let s = async (o, i) => {
60
- let l = { ...i, ...o }, x = await n(l);
50
+ let t = async (s, i) => {
51
+ let c = { ...i, ...s }, l = await n(c);
61
52
  return {
62
53
  ...i,
63
- [e]: x
54
+ [e]: l
64
55
  };
65
56
  };
66
- return new t(
67
- this.name,
68
- [...this.resolvers, s],
69
- this.hooks,
70
- this.defaultConfig,
71
- this._extensionFn
72
- );
57
+ return this.resolvers = [...this.resolvers, t], this;
73
58
  }
74
59
  on(e, n) {
75
- let s = this.hooks[e] || [], o = {
60
+ let t = this.hooks[e] || [];
61
+ return this.hooks = {
76
62
  ...this.hooks,
77
- [e]: [...s, n]
78
- };
79
- return new t(
80
- this.name,
81
- this.resolvers,
82
- o,
83
- this.defaultConfig,
84
- this._extensionFn
85
- );
63
+ [e]: [...t, n]
64
+ }, this;
86
65
  }
87
66
  extends(e) {
88
- return new t(
89
- this.name,
90
- this.resolvers,
91
- this.hooks,
92
- this.defaultConfig,
93
- (n, s) => ({ ...this._extensionFn ? this._extensionFn(n, s) : {}, ...e(n, s) })
94
- );
67
+ let n = this._extensionFn;
68
+ return this._extensionFn = (t, s) => ({ ...n ? n(t, s) : {}, ...e(t, s) }), this;
95
69
  }
96
70
  _build(e) {
97
71
  let n = { ...this.defaultConfig, ...e };
@@ -104,18 +78,7 @@ var a = class t {
104
78
  };
105
79
  }
106
80
  static create(e) {
107
- return new t(e.name, [], {}, {}, void 0);
108
- }
109
- static instance() {
110
- return {
111
- create: (e) => new t(
112
- e.name,
113
- [],
114
- {},
115
- {},
116
- void 0
117
- )
118
- };
81
+ return new o(e.name, [], {}, {}, void 0);
119
82
  }
120
83
  };
121
84
  0 && (module.exports = {
package/dist/mjs/index.js CHANGED
@@ -1,76 +1,50 @@
1
1
  // packages/plugin/src/controllers/Plugin.ts
2
- var r = class s {
2
+ var i = class r {
3
3
  name;
4
4
  resolvers;
5
5
  defaultConfig;
6
- hooks;
6
+ hooks = {};
7
7
  instance;
8
8
  _extensionFn;
9
- constructor(e, n, t, o, i) {
10
- this.name = e, this.resolvers = n, this.hooks = t, this.defaultConfig = o, this._extensionFn = i;
9
+ constructor(e, n, t, s, o) {
10
+ this.name = e, this.resolvers = n, this.hooks = t, this.defaultConfig = s, this._extensionFn = o;
11
11
  }
12
12
  config(e) {
13
- return new s(
14
- this.name,
15
- this.resolvers,
16
- this.hooks,
17
- e,
18
- this._extensionFn
19
- );
13
+ return this.defaultConfig = {
14
+ ...this.defaultConfig,
15
+ defaultConfig: e
16
+ }, this;
20
17
  }
21
18
  defineInstance(e) {
22
19
  return this.instance = e, this;
23
20
  }
24
21
  decorate(e, n) {
25
- let t = async (o, i) => ({
26
- ...i,
22
+ let t = async (s, o) => ({
23
+ ...o,
27
24
  [e]: n
28
25
  });
29
- return new s(
30
- this.name,
31
- [...this.resolvers, t],
32
- this.hooks,
33
- this.defaultConfig,
34
- this._extensionFn
35
- );
26
+ return this.resolvers = [...this.resolvers, t], this;
36
27
  }
37
28
  derive(e, n) {
38
- let t = async (o, i) => {
39
- let a = { ...i, ...o }, l = await n(a);
29
+ let t = async (s, o) => {
30
+ let a = { ...o, ...s }, c = await n(a);
40
31
  return {
41
- ...i,
42
- [e]: l
32
+ ...o,
33
+ [e]: c
43
34
  };
44
35
  };
45
- return new s(
46
- this.name,
47
- [...this.resolvers, t],
48
- this.hooks,
49
- this.defaultConfig,
50
- this._extensionFn
51
- );
36
+ return this.resolvers = [...this.resolvers, t], this;
52
37
  }
53
38
  on(e, n) {
54
- let t = this.hooks[e] || [], o = {
39
+ let t = this.hooks[e] || [];
40
+ return this.hooks = {
55
41
  ...this.hooks,
56
42
  [e]: [...t, n]
57
- };
58
- return new s(
59
- this.name,
60
- this.resolvers,
61
- o,
62
- this.defaultConfig,
63
- this._extensionFn
64
- );
43
+ }, this;
65
44
  }
66
45
  extends(e) {
67
- return new s(
68
- this.name,
69
- this.resolvers,
70
- this.hooks,
71
- this.defaultConfig,
72
- (n, t) => ({ ...this._extensionFn ? this._extensionFn(n, t) : {}, ...e(n, t) })
73
- );
46
+ let n = this._extensionFn;
47
+ return this._extensionFn = (t, s) => ({ ...n ? n(t, s) : {}, ...e(t, s) }), this;
74
48
  }
75
49
  _build(e) {
76
50
  let n = { ...this.defaultConfig, ...e };
@@ -83,20 +57,9 @@ var r = class s {
83
57
  };
84
58
  }
85
59
  static create(e) {
86
- return new s(e.name, [], {}, {}, void 0);
87
- }
88
- static instance() {
89
- return {
90
- create: (e) => new s(
91
- e.name,
92
- [],
93
- {},
94
- {},
95
- void 0
96
- )
97
- };
60
+ return new r(e.name, [], {}, {}, void 0);
98
61
  }
99
62
  };
100
63
  export {
101
- r as Plugin
64
+ i as Plugin
102
65
  };
@@ -1,17 +1,20 @@
1
- import type { AnyAsterflow, ExtendedAsterflow } from '@asterflow/core';
1
+ import type { AnyAsterflow, ExtendedAsterflow } from 'asterflow';
2
2
  import type { PluginHooks, Resolver } from '../types/plugin';
3
- export declare class Plugin<Path extends string, Instance extends AnyAsterflow, Config extends Record<string, any> = {}, Context extends Record<string, any> = {}, Hooks extends PluginHooks<any, Context[], any> = {}, Extension extends Record<string, any> = {}> {
3
+ import type { Prettify, UnionToIntersection } from '../types/utils';
4
+ export declare class Plugin<Path extends string = string, Instance extends AnyAsterflow = AnyAsterflow, Config extends Record<string, any> = {}, Decorate extends Record<string, any> = {}, Derive extends Record<string, any> = {}, Extension extends Record<string, any> = {}> {
4
5
  readonly name: Path;
5
- private readonly resolvers;
6
- readonly defaultConfig: Partial<Config>;
7
- readonly hooks: Hooks;
6
+ resolvers: Resolver[];
7
+ defaultConfig: Partial<Config>;
8
+ hooks: PluginHooks<any, Decorate, any>;
8
9
  instance: Instance;
9
- private readonly _extensionFn?;
10
+ private _extensionFn?;
10
11
  private constructor();
11
12
  /**
12
13
  * Defines the shape of the configuration and its default values for this plugin.
13
14
  */
14
- config<C extends Record<string, any>>(defaultConfig: C): Plugin<Path, Instance, C, Context, Hooks, Extension>;
15
+ config<C extends Record<string, any>>(defaultConfig: C): Plugin<Path, Instance, Prettify<UnionToIntersection<{
16
+ defaultConfig: C;
17
+ } | C | Config>>, Decorate, Derive, Extension>;
15
18
  /**
16
19
  * O `defineInstance` agora é mais simples. Ele não precisa mais re-tipar
17
20
  * a classe inteira. Ele só serve para passar o `this` para o `_build`.
@@ -20,12 +23,12 @@ export declare class Plugin<Path extends string, Instance extends AnyAsterflow,
20
23
  /**
21
24
  * Adds a new static value to the plugin's context (decoration).
22
25
  */
23
- decorate<Key extends string, Value>(key: Key, value: Value): Plugin<Path, Instance, Config, Context & { [K in Key]: Value; }, Hooks, Extension>;
26
+ decorate<Key extends string, Value>(key: Key, value: Value): Plugin<Path, Instance, Config, Prettify<UnionToIntersection<Decorate | { [K in Key]: Value; }>>, Derive, Extension>;
24
27
  /**
25
28
  * Adds a new property to the context that is derived from the configuration and the existing context.
26
29
  * The resolver function is executed lazily when the plugin is registered via `app.use()`.
27
30
  */
28
- derive<Key extends string, Value>(key: Key, resolverFn: (context: Context & Config) => Value | Promise<Value>): Plugin<Path, Instance, Config, Context & { [K in Key]: Awaited<Value>; }, Hooks, Extension>;
31
+ derive<Key extends string, Value>(key: Key, resolverFn: (context: Derive & Config & Decorate) => Value | Promise<Value>): Plugin<Path, Instance, Config, Decorate, Prettify<UnionToIntersection<Derive | { [K in Key]: Awaited<Value>; }>>, Extension>;
29
32
  /**
30
33
  * Registers a handler for a specific lifecycle event.
31
34
  * Adding a hook makes the plugin "runtime-aware". AsterFlow can optimize by only
@@ -36,21 +39,21 @@ export declare class Plugin<Path extends string, Instance extends AnyAsterflow,
36
39
  * .on('beforeInitialize', (app, context) => { })
37
40
  * .on('beforeInitialize', (app, context) => { });
38
41
  */
39
- on<Event extends keyof PluginHooks<ExtendedAsterflow<Instance>, Context & Config, Extension>>(event: Event, handler: NonNullable<PluginHooks<ExtendedAsterflow<Instance>, Context & Config, Extension>[Event]>[number]): Plugin<Path, Instance, Config, Context, Omit<Hooks, Event> & { [K in Event]: [...Hooks extends { [k in Event]: any[]; } ? Hooks[K] : []]; }, Extension>;
42
+ on<Event extends keyof PluginHooks<ExtendedAsterflow<Instance>, Prettify<UnionToIntersection<Derive | Config | Decorate>>, Extension>>(event: Event, handler: NonNullable<PluginHooks<ExtendedAsterflow<Instance>, Prettify<UnionToIntersection<Derive | Config | Decorate>>, Extension>[Event]>[number]): this;
40
43
  /**
41
44
  * Defines new properties or methods to be added to the AsterFlow instance.
42
45
  * A função recebe a instância do app e o contexto do plugin, e deve retornar um objeto
43
46
  * com as novas propriedades.
44
47
  */
45
- extends<E extends Record<string, any>>(extensionFn: (app: ExtendedAsterflow<Instance>, context: Context) => E): Plugin<Path, ExtendedAsterflow<Instance>, Config, Context, Hooks, Extension & E>;
48
+ extends<E extends Record<string, any>>(extensionFn: (app: Instance, context: Prettify<UnionToIntersection<Config | Derive | Decorate>>) => E): Plugin<Path, Instance, Config, Decorate, Derive, Prettify<UnionToIntersection<Extension & E>>>;
46
49
  /**
47
50
  * Builds the final context and hooks from the provided configuration.
48
51
  */
49
52
  _build(config: any): {
50
53
  name: Path;
51
- context: Config & Context;
52
- hooks: Hooks;
53
- _extensionFn: ((app: Instance, context: Context) => Extension) | undefined;
54
+ context: Config & Derive & Decorate;
55
+ hooks: PluginHooks<any, Decorate, any>;
56
+ _extensionFn: ((app: Instance, context: Prettify<UnionToIntersection<Config & Decorate & Derive>>) => Extension) | undefined;
54
57
  resolvers: Resolver[];
55
58
  };
56
59
  /**
@@ -58,10 +61,5 @@ export declare class Plugin<Path extends string, Instance extends AnyAsterflow,
58
61
  */
59
62
  static create<Path extends string, Asterflow extends AnyAsterflow>(options: {
60
63
  name: Path;
61
- }): Plugin<Path, Asterflow, {}, {}, {}>;
62
- static instance<T extends AnyAsterflow>(): {
63
- create: <Path extends string>(options: {
64
- name: Path;
65
- }) => Plugin<Path, T, {}, {}, {}, {}>;
66
- };
64
+ }): Plugin<Path, Asterflow, {}, {}, {}, {}>;
67
65
  }
@@ -1,16 +1,18 @@
1
1
  import type { Runtime } from '@asterflow/adapter';
2
- import type { AnyAsterflow, ExtendedAsterflow } from '@asterflow/core';
2
+ import type { AnyAsterflow, ExtendedAsterflow } from 'asterflow';
3
3
  import type { Request } from '@asterflow/request';
4
- import type { AsterResponse } from '@asterflow/response';
4
+ import type { AsterResponse, Prettify } from '@asterflow/response';
5
5
  import type { Plugin } from '../controllers/Plugin';
6
- export type AnyPlugin = Plugin<string, any, any, any, any, any>;
6
+ import type { AnyRecord, UnionToIntersection } from './utils';
7
+ export type AnyPluginHooks = PluginHooks<AnyAsterflow, AnyRecord, AnyRecord>;
8
+ export type AnyPlugin = Plugin<string, AnyAsterflow, AnyRecord, AnyRecord, AnyRecord, AnyRecord>;
7
9
  export type AnyPlugins = Record<string, ResolvedPlugin<AnyPlugin>>;
8
- export type AnyPluginHooks = PluginHooks<any, any, any>;
9
10
  export type AnyPluginInstance = ResolvedPlugin<AnyPlugin> & {
10
11
  hooks: AnyPluginHooks;
11
12
  };
12
13
  export type InferPluginExtension<P> = P extends Plugin<any, any, any, any, any, infer Ext> ? Ext : {};
13
- export type ResolvedPlugin<P extends AnyPlugin> = P extends Plugin<infer Path, any, any, infer Ctx, any, infer Ext> ? {
14
+ export type InferPluginContext<P> = P extends Plugin<any, any, infer Config, infer Decorate, infer Derive, any> ? Prettify<UnionToIntersection<Config | Decorate | Derive>> : {};
15
+ export type ResolvedPlugin<P extends Plugin<any, any, any, any, any, any>> = P extends Plugin<infer Path, any, any, infer Ctx, any, infer Ext> ? {
14
16
  name: Path;
15
17
  context: Ctx;
16
18
  hooks: PluginHooks<any, Ctx, Ext>;
@@ -21,23 +23,23 @@ export type ResolvedPlugin<P extends AnyPlugin> = P extends Plugin<infer Path, a
21
23
  * Tipo para extrair o objeto de configuração de um plugin.
22
24
  * Ele torna as propriedades com valores padrão opcionais.
23
25
  */
24
- export type InferConfigArgument<P extends AnyPlugin> = P extends Plugin<any, any, infer C, any, any> ? Omit<C, keyof P['defaultConfig']> & Partial<Pick<C, keyof P['defaultConfig'] & keyof C>> : never;
26
+ export type InferConfigArgument<P extends Plugin<any, any, any, any, any, any>> = P extends Plugin<any, any, infer C, any, any, any> ? Omit<C, keyof P['defaultConfig']> & Partial<Pick<C, keyof P['defaultConfig'] & keyof C>> : never;
25
27
  /**
26
28
  * Defines the available lifecycle hooks a plugin can register.
27
29
  */
28
30
  export type PluginHooks<Instance extends AnyAsterflow, Context extends Record<string, any>, Extension extends Record<string, any> = {}> = {
29
- beforeInitialize?: ((app: ExtendedAsterflow<Instance> & Extension, context: Context) => void | Promise<void>)[];
30
- afterInitialize?: ((app: ExtendedAsterflow<Instance> & Extension, context: Context) => void | Promise<void>)[];
31
+ beforeInitialize?: ((app: ExtendedAsterflow<Instance> & Extension, context: Context) => any | Promise<any>)[];
32
+ afterInitialize?: ((app: ExtendedAsterflow<Instance> & Extension, context: Context) => any | Promise<any>)[];
31
33
  onRequest?: (({ context, request, response }: {
32
34
  request: Request<Runtime>;
33
35
  response: AsterResponse;
34
36
  context: Context;
35
- }) => void | Promise<void>)[];
37
+ }) => any | Promise<any>)[];
36
38
  onResponse?: (({ context, request, response }: {
37
39
  request: Request<Runtime>;
38
40
  response: AsterResponse;
39
41
  context: Context;
40
- }) => void | Promise<void>)[];
42
+ }) => any | Promise<any>)[];
41
43
  };
42
44
  /**
43
45
  * Represents a function that resolves a part of the plugin's context.
@@ -0,0 +1,5 @@
1
+ export type AnyRecord = Record<string, any>;
2
+ export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never;
3
+ export type Prettify<T> = {
4
+ [K in keyof T]: T[K];
5
+ } & {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asterflow/plugin",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "main": "dist/cjs/index.cjs",
5
5
  "module": "dist/mjs/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -18,9 +18,9 @@
18
18
  "node": ">=20"
19
19
  },
20
20
  "devDependencies": {
21
- "@asterflow/core": "1.0.8",
22
- "@asterflow/response": "1.0.5",
23
- "@asterflow/request": "1.0.8"
21
+ "asterflow": "0.0.1",
22
+ "@asterflow/response": "1.0.6",
23
+ "@asterflow/request": "1.0.9"
24
24
  },
25
25
  "peerDependencies": {
26
26
  "typescript": "^5.8.3"