asterflow 0.0.4 → 0.0.6

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.
@@ -1,35 +1,35 @@
1
1
  "use strict";
2
- var c = Object.defineProperty;
3
- var g = Object.getOwnPropertyDescriptor;
2
+ var y = Object.defineProperty;
3
+ var x = Object.getOwnPropertyDescriptor;
4
4
  var P = Object.getOwnPropertyNames;
5
5
  var w = Object.prototype.hasOwnProperty;
6
- var A = (d, e) => {
6
+ var A = (u, e) => {
7
7
  for (var t in e)
8
- c(d, t, { get: e[t], enumerable: !0 });
9
- }, M = (d, e, t, s) => {
8
+ y(u, t, { get: e[t], enumerable: !0 });
9
+ }, M = (u, e, t, s) => {
10
10
  if (e && typeof e == "object" || typeof e == "function")
11
11
  for (let n of P(e))
12
- !w.call(d, n) && n !== t && c(d, n, { get: () => e[n], enumerable: !(s = g(e, n)) || s.enumerable });
13
- return d;
12
+ !w.call(u, n) && n !== t && y(u, n, { get: () => e[n], enumerable: !(s = x(e, n)) || s.enumerable });
13
+ return u;
14
14
  };
15
- var I = (d) => M(c({}, "__esModule", { value: !0 }), d);
15
+ var I = (u) => M(y({}, "__esModule", { value: !0 }), u);
16
16
  // core/src/index.ts
17
17
  var C = {};
18
18
  A(C, {
19
19
  AsterFlow: () => v,
20
- AsterFlowInstance: () => h
20
+ AsterFlowInstance: () => c
21
21
  });
22
22
  module.exports = I(C);
23
23
  // core/src/controllers/Asterflow.ts
24
- var p = require("@asterflow/adapter"), m = require("@asterflow/response"), u = require("@asterflow/router"), l = require("@asterflow/url-parser"), x = require("reminist");
24
+ var f = require("@asterflow/adapter"), m = require("@asterflow/response"), l = require("@asterflow/router"), R = require("@asterflow/url-parser"), g = require("reminist");
25
25
  // core/src/utils/parser.ts
26
- function y(d, e) {
27
- return `${d}${e}`.replace(/\/{2,}/g, "/");
26
+ function p(u, e) {
27
+ return `${u}${e}`.replace(/\/{2,}/g, "/");
28
28
  }
29
29
  // core/src/controllers/Asterflow.ts
30
- var h = class {
30
+ var c = class {
31
31
  driver;
32
- reminist = new x.Reminist({ keys: Object.keys(u.MethodType) });
32
+ reminist = new g.Reminist({ keys: Object.keys(l.MethodType) });
33
33
  middlewares = [];
34
34
  plugins = {};
35
35
  onRequestPlugins = [];
@@ -37,56 +37,55 @@ var h = class {
37
37
  beforeInitializePlugins = [];
38
38
  afterInitializePlugins = [];
39
39
  constructor(e) {
40
- this.driver = e?.driver ?? p.adapters.node, this.driver.onRequest = this.handleRequest.bind(this);
40
+ this.driver = e?.driver ?? f.adapters.node, this.driver.onRequest = this.handleRequest.bind(this);
41
41
  }
42
42
  async handleRequest(e, t) {
43
43
  t = t ?? new m.AsterResponse();
44
- let s = await this.runHooks("onRequest", e, t);
45
- if (s) return s;
46
- let n = () => t.notFound({
44
+ let s = () => t.notFound({
47
45
  statusCode: 404,
48
46
  code: "NOT_FOUND",
49
47
  message: `Unable to find route: ${e.getPathname()}`
50
- }), o = e.getMethod().toLowerCase();
51
- if (!o) return n();
52
- let r = this.reminist.find(o, e.url.getPathname());
53
- if (!r?.node?.store) return n();
54
- let i = r.node.store;
55
- (i.url.ast.expressions.has(l.InternalExpression.Variable) || i.url.ast.expressions.has(l.InternalExpression.Slug)) && (e.url = e.url.withParser(i.url));
48
+ }), n = e.getMethod().toLowerCase();
49
+ if (!n) return s();
50
+ let r = this.reminist.find(n, e.url.getPathname());
51
+ if (!r?.node?.store) return s();
52
+ let i = r.node.store, a = await this.runHooks("onRequest", this, i, e, t);
53
+ if (a) return a;
54
+ (i.url.ast.expressions.has(R.InternalExpression.Variable) || i.url.ast.expressions.has(R.InternalExpression.Slug)) && (e.url = e.url.withParser(i.url));
56
55
  try {
57
- return await this.runHandler(i, e, t), await this.runHooks("onResponse", e, t), t;
58
- } catch (a) {
59
- return console.log(a), t.badRequest({
56
+ return await this.runHandler(i, e, t), await this.runHooks("onResponse", this, i, e, t), t;
57
+ } catch (o) {
58
+ return console.log(o), t.badRequest({
60
59
  statusCode: 400,
61
- message: a instanceof l.ErrorLog ? "AST_ERROR" : "ERROR",
62
- error: a instanceof l.ErrorLog ? a.message : a
60
+ message: o instanceof R.ErrorLog ? "AST_ERROR" : "ERROR",
61
+ error: o instanceof R.ErrorLog || o instanceof Error ? o.message : o
63
62
  });
64
63
  }
65
64
  }
66
65
  middleware(e) {
67
66
  for (let t of e.controllers) {
68
- let s = y(e.basePath, t.path);
67
+ let s = p(e.basePath, t.path);
69
68
  this.addEntry(t, s);
70
69
  }
71
70
  return this;
72
71
  }
73
72
  controller(e) {
74
- let t = y("/", e.path);
73
+ let t = p("/", e.path);
75
74
  return this.addEntry(e, t), this;
76
75
  }
77
76
  use(e, t) {
78
77
  let n = e.defineInstance(this)._build(t);
79
78
  if (this.plugins[n.name] = n, n.hooks.onRequest && this.onRequestPlugins.push(n), n.hooks.onResponse && this.onResponsePlugins.push(n), n.hooks.beforeInitialize && this.beforeInitializePlugins.push(n), n.hooks.afterInitialize && this.afterInitializePlugins.push(n), n._extensionFn) {
80
- let o = n._extensionFn(this, n.context);
81
- Object.assign(this, o);
79
+ let r = n._extensionFn(this, n.context);
80
+ Object.assign(this, r);
82
81
  }
83
82
  return this;
84
83
  }
85
84
  router(e) {
86
- return this.controller(new u.Router(e)), this;
85
+ return this.controller(new l.Router(e)), this;
87
86
  }
88
87
  method(e) {
89
- return this.controller(new u.Method(e)), this;
88
+ return this.controller(new l.Method(e)), this;
90
89
  }
91
90
  async resolvePluginContexts() {
92
91
  for (let e in this.plugins) {
@@ -99,74 +98,74 @@ var h = class {
99
98
  }
100
99
  }
101
100
  async listen(...e) {
102
- await this.resolvePluginContexts(), await this.runHooks("beforeInitialize"), await this.driver.listen(...e), await this.runHooks("afterInitialize");
101
+ await this.resolvePluginContexts(), await this.runHooks("beforeInitialize", this), await this.driver.listen(...e), await this.runHooks("afterInitialize", this);
103
102
  }
104
103
  addEntry(e, t) {
105
- let s = e instanceof u.Method ? [e.method] : Object.keys(e.methods), n = { path: t, route: e, methods: s, url: new l.Analyze(t) };
106
- for (let o of s)
107
- this.reminist.add(o, t, n);
104
+ let s = e instanceof l.Method ? [e.method] : Object.keys(e.methods), n = { path: t, route: e, methods: s, url: new R.Analyze(t) };
105
+ for (let r of s)
106
+ this.reminist.add(r, t, n);
108
107
  }
109
- async runHooks(e, t, s) {
110
- let n = this[`${e}Plugins`];
111
- for (let o of n)
108
+ async runHooks(e, t, s, n, r) {
109
+ let i = this[`${e}Plugins`];
110
+ for (let a of i)
112
111
  switch (e) {
113
112
  case "beforeInitialize":
114
113
  case "afterInitialize":
115
114
  {
116
- let r = o.hooks[e];
117
- if (r)
118
- for (let i of r)
119
- await i(this, o.context);
115
+ let o = a.hooks[e];
116
+ if (!o) break;
117
+ for (let d of o)
118
+ await d(this, a.context);
120
119
  }
121
120
  break;
122
121
  case "onRequest":
123
122
  {
124
- if (!t || !s) return;
125
- let r = o.hooks.onRequest;
126
- if (r)
127
- for (let i of r) {
128
- let a = await i({ request: t, response: s, context: o.context });
129
- if (a && typeof a == "object" && a.constructor?.name === "AsterResponse")
130
- return a;
131
- }
123
+ let o = a.hooks.onRequest;
124
+ if (!o) break;
125
+ if (!n || !r || !s) return;
126
+ for (let d of o) {
127
+ let h = await d({ instance: t, router: s, request: n, response: r, plugin: a });
128
+ if (h && h instanceof m.AsterResponse)
129
+ return h;
130
+ }
132
131
  }
133
132
  break;
134
133
  case "onResponse":
135
134
  {
136
- if (!t || !s) return;
137
- let r = o.hooks.onResponse;
138
- if (r)
139
- for (let i of r)
140
- await i({ request: t, response: s, context: o.context });
135
+ if (!n || !r || !s) return;
136
+ let o = a.hooks.onResponse;
137
+ if (o)
138
+ for (let d of o)
139
+ await d({ instance: t, router: s, request: n, response: r, plugin: a });
141
140
  }
142
141
  break;
143
142
  }
144
143
  }
145
144
  async runHandler({ route: e }, t, s) {
146
- let n = t.getMethod().toLowerCase(), o = e instanceof u.Method ? e.handler : e.methods[n], r = e instanceof u.Method ? e.schema : e.schema?.[n];
147
- if (!o) return null;
148
- if (r) {
149
- let R = r.safeParse(await t.getBody());
150
- if (!R.success)
145
+ let n = t.getMethod().toLowerCase(), r = e instanceof l.Method ? e.handler : e.methods[n], i = e instanceof l.Method ? e.schema : e.schema?.[n];
146
+ if (!r) return null;
147
+ if (i) {
148
+ let d = i.safeParse(await t.getBody());
149
+ if (!d.success)
151
150
  return s.validationError({
152
151
  statusCode: 422,
153
152
  message: "VALIDATION_ERROR",
154
- error: R.error
153
+ error: JSON.parse(d.error)
155
154
  });
156
- s.send(R.data);
155
+ s.send(d.data);
157
156
  }
158
- let i = Object.values(this.plugins).reduce((R, f) => ({ ...R, ...f.context }), {}), a = {
157
+ let a = Object.values(this.plugins).reduce((d, h) => ({ ...d, ...h.context }), {}), o = {
159
158
  instance: this,
160
159
  request: t,
161
160
  response: s,
162
161
  url: t.url,
163
162
  schema: await t.getBody(),
164
163
  middleware: {},
165
- plugins: i
164
+ plugins: a
166
165
  };
167
- return o(a);
166
+ return r(o);
168
167
  }
169
- }, v = h;
168
+ }, v = c;
170
169
  0 && (module.exports = {
171
170
  AsterFlow,
172
171
  AsterFlowInstance
package/dist/mjs/index.js CHANGED
@@ -1,21 +1,21 @@
1
1
  // core/src/controllers/Asterflow.ts
2
- import { adapters as m } from "@asterflow/adapter";
3
- import { AsterResponse as x } from "@asterflow/response";
2
+ import { adapters as f } from "@asterflow/adapter";
3
+ import { AsterResponse as y } from "@asterflow/response";
4
4
  import {
5
- Method as u,
6
- MethodType as f,
7
- Router as g
5
+ Method as l,
6
+ MethodType as g,
7
+ Router as x
8
8
  } from "@asterflow/router";
9
- import { Analyze as P, ErrorLog as c, InternalExpression as y } from "@asterflow/url-parser";
9
+ import { Analyze as P, ErrorLog as p, InternalExpression as m } from "@asterflow/url-parser";
10
10
  import { Reminist as w } from "reminist";
11
11
  // core/src/utils/parser.ts
12
- function l(h, e) {
13
- return `${h}${e}`.replace(/\/{2,}/g, "/");
12
+ function R(c, e) {
13
+ return `${c}${e}`.replace(/\/{2,}/g, "/");
14
14
  }
15
15
  // core/src/controllers/Asterflow.ts
16
- var R = class {
16
+ var h = class {
17
17
  driver;
18
- reminist = new w({ keys: Object.keys(f) });
18
+ reminist = new w({ keys: Object.keys(g) });
19
19
  middlewares = [];
20
20
  plugins = {};
21
21
  onRequestPlugins = [];
@@ -23,56 +23,55 @@ var R = class {
23
23
  beforeInitializePlugins = [];
24
24
  afterInitializePlugins = [];
25
25
  constructor(e) {
26
- this.driver = e?.driver ?? m.node, this.driver.onRequest = this.handleRequest.bind(this);
26
+ this.driver = e?.driver ?? f.node, this.driver.onRequest = this.handleRequest.bind(this);
27
27
  }
28
28
  async handleRequest(e, t) {
29
- t = t ?? new x();
30
- let s = await this.runHooks("onRequest", e, t);
31
- if (s) return s;
32
- let n = () => t.notFound({
29
+ t = t ?? new y();
30
+ let s = () => t.notFound({
33
31
  statusCode: 404,
34
32
  code: "NOT_FOUND",
35
33
  message: `Unable to find route: ${e.getPathname()}`
36
- }), o = e.getMethod().toLowerCase();
37
- if (!o) return n();
38
- let r = this.reminist.find(o, e.url.getPathname());
39
- if (!r?.node?.store) return n();
40
- let i = r.node.store;
41
- (i.url.ast.expressions.has(y.Variable) || i.url.ast.expressions.has(y.Slug)) && (e.url = e.url.withParser(i.url));
34
+ }), n = e.getMethod().toLowerCase();
35
+ if (!n) return s();
36
+ let r = this.reminist.find(n, e.url.getPathname());
37
+ if (!r?.node?.store) return s();
38
+ let i = r.node.store, a = await this.runHooks("onRequest", this, i, e, t);
39
+ if (a) return a;
40
+ (i.url.ast.expressions.has(m.Variable) || i.url.ast.expressions.has(m.Slug)) && (e.url = e.url.withParser(i.url));
42
41
  try {
43
- return await this.runHandler(i, e, t), await this.runHooks("onResponse", e, t), t;
44
- } catch (a) {
45
- return console.log(a), t.badRequest({
42
+ return await this.runHandler(i, e, t), await this.runHooks("onResponse", this, i, e, t), t;
43
+ } catch (o) {
44
+ return console.log(o), t.badRequest({
46
45
  statusCode: 400,
47
- message: a instanceof c ? "AST_ERROR" : "ERROR",
48
- error: a instanceof c ? a.message : a
46
+ message: o instanceof p ? "AST_ERROR" : "ERROR",
47
+ error: o instanceof p || o instanceof Error ? o.message : o
49
48
  });
50
49
  }
51
50
  }
52
51
  middleware(e) {
53
52
  for (let t of e.controllers) {
54
- let s = l(e.basePath, t.path);
53
+ let s = R(e.basePath, t.path);
55
54
  this.addEntry(t, s);
56
55
  }
57
56
  return this;
58
57
  }
59
58
  controller(e) {
60
- let t = l("/", e.path);
59
+ let t = R("/", e.path);
61
60
  return this.addEntry(e, t), this;
62
61
  }
63
62
  use(e, t) {
64
63
  let n = e.defineInstance(this)._build(t);
65
64
  if (this.plugins[n.name] = n, n.hooks.onRequest && this.onRequestPlugins.push(n), n.hooks.onResponse && this.onResponsePlugins.push(n), n.hooks.beforeInitialize && this.beforeInitializePlugins.push(n), n.hooks.afterInitialize && this.afterInitializePlugins.push(n), n._extensionFn) {
66
- let o = n._extensionFn(this, n.context);
67
- Object.assign(this, o);
65
+ let r = n._extensionFn(this, n.context);
66
+ Object.assign(this, r);
68
67
  }
69
68
  return this;
70
69
  }
71
70
  router(e) {
72
- return this.controller(new g(e)), this;
71
+ return this.controller(new x(e)), this;
73
72
  }
74
73
  method(e) {
75
- return this.controller(new u(e)), this;
74
+ return this.controller(new l(e)), this;
76
75
  }
77
76
  async resolvePluginContexts() {
78
77
  for (let e in this.plugins) {
@@ -85,75 +84,75 @@ var R = class {
85
84
  }
86
85
  }
87
86
  async listen(...e) {
88
- await this.resolvePluginContexts(), await this.runHooks("beforeInitialize"), await this.driver.listen(...e), await this.runHooks("afterInitialize");
87
+ await this.resolvePluginContexts(), await this.runHooks("beforeInitialize", this), await this.driver.listen(...e), await this.runHooks("afterInitialize", this);
89
88
  }
90
89
  addEntry(e, t) {
91
- let s = e instanceof u ? [e.method] : Object.keys(e.methods), n = { path: t, route: e, methods: s, url: new P(t) };
92
- for (let o of s)
93
- this.reminist.add(o, t, n);
90
+ let s = e instanceof l ? [e.method] : Object.keys(e.methods), n = { path: t, route: e, methods: s, url: new P(t) };
91
+ for (let r of s)
92
+ this.reminist.add(r, t, n);
94
93
  }
95
- async runHooks(e, t, s) {
96
- let n = this[`${e}Plugins`];
97
- for (let o of n)
94
+ async runHooks(e, t, s, n, r) {
95
+ let i = this[`${e}Plugins`];
96
+ for (let a of i)
98
97
  switch (e) {
99
98
  case "beforeInitialize":
100
99
  case "afterInitialize":
101
100
  {
102
- let r = o.hooks[e];
103
- if (r)
104
- for (let i of r)
105
- await i(this, o.context);
101
+ let o = a.hooks[e];
102
+ if (!o) break;
103
+ for (let d of o)
104
+ await d(this, a.context);
106
105
  }
107
106
  break;
108
107
  case "onRequest":
109
108
  {
110
- if (!t || !s) return;
111
- let r = o.hooks.onRequest;
112
- if (r)
113
- for (let i of r) {
114
- let a = await i({ request: t, response: s, context: o.context });
115
- if (a && typeof a == "object" && a.constructor?.name === "AsterResponse")
116
- return a;
117
- }
109
+ let o = a.hooks.onRequest;
110
+ if (!o) break;
111
+ if (!n || !r || !s) return;
112
+ for (let d of o) {
113
+ let u = await d({ instance: t, router: s, request: n, response: r, plugin: a });
114
+ if (u && u instanceof y)
115
+ return u;
116
+ }
118
117
  }
119
118
  break;
120
119
  case "onResponse":
121
120
  {
122
- if (!t || !s) return;
123
- let r = o.hooks.onResponse;
124
- if (r)
125
- for (let i of r)
126
- await i({ request: t, response: s, context: o.context });
121
+ if (!n || !r || !s) return;
122
+ let o = a.hooks.onResponse;
123
+ if (o)
124
+ for (let d of o)
125
+ await d({ instance: t, router: s, request: n, response: r, plugin: a });
127
126
  }
128
127
  break;
129
128
  }
130
129
  }
131
130
  async runHandler({ route: e }, t, s) {
132
- let n = t.getMethod().toLowerCase(), o = e instanceof u ? e.handler : e.methods[n], r = e instanceof u ? e.schema : e.schema?.[n];
133
- if (!o) return null;
134
- if (r) {
135
- let d = r.safeParse(await t.getBody());
131
+ let n = t.getMethod().toLowerCase(), r = e instanceof l ? e.handler : e.methods[n], i = e instanceof l ? e.schema : e.schema?.[n];
132
+ if (!r) return null;
133
+ if (i) {
134
+ let d = i.safeParse(await t.getBody());
136
135
  if (!d.success)
137
136
  return s.validationError({
138
137
  statusCode: 422,
139
138
  message: "VALIDATION_ERROR",
140
- error: d.error
139
+ error: JSON.parse(d.error)
141
140
  });
142
141
  s.send(d.data);
143
142
  }
144
- let i = Object.values(this.plugins).reduce((d, p) => ({ ...d, ...p.context }), {}), a = {
143
+ let a = Object.values(this.plugins).reduce((d, u) => ({ ...d, ...u.context }), {}), o = {
145
144
  instance: this,
146
145
  request: t,
147
146
  response: s,
148
147
  url: t.url,
149
148
  schema: await t.getBody(),
150
149
  middleware: {},
151
- plugins: i
150
+ plugins: a
152
151
  };
153
- return o(a);
152
+ return r(o);
154
153
  }
155
- }, k = R;
154
+ }, k = h;
156
155
  export {
157
156
  k as AsterFlow,
158
- R as AsterFlowInstance
157
+ h as AsterFlowInstance
159
158
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "asterflow",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "main": "dist/cjs/index.cjs",
5
5
  "module": "dist/mjs/index.js",
6
6
  "types": "dist/types/index.d.ts",
@@ -34,10 +34,10 @@
34
34
  "typescript": "^5.8.3"
35
35
  },
36
36
  "dependencies": {
37
- "@asterflow/adapter": "1.0.12",
38
- "@asterflow/plugin": "1.0.8",
39
- "@asterflow/response": "1.0.9",
40
- "@asterflow/router": "1.0.12",
37
+ "@asterflow/adapter": "1.0.14",
38
+ "@asterflow/plugin": "1.0.10",
39
+ "@asterflow/response": "1.0.11",
40
+ "@asterflow/router": "1.0.14",
41
41
  "@asterflow/url-parser": "^2.0.3",
42
42
  "reminist": "^1.0.5"
43
43
  }