Skip to content

Commit fb0ca90

Browse files
committed
ci: build dist on commit and push to *-dist
1 parent 52525d9 commit fb0ca90

File tree

10 files changed

+6248
-42
lines changed

10 files changed

+6248
-42
lines changed

.github/workflows/build.yml

Lines changed: 0 additions & 29 deletions
This file was deleted.

.github/workflows/main.yml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ on:
44
push:
55
branches:
66
- canary
7+
- main
78
pull_request:
89

910
jobs:
@@ -21,7 +22,10 @@ jobs:
2122
uses: actions/setup-node@v4
2223
with:
2324
node-version: ${{ matrix.node }}
24-
cache: 'npm'
25+
cache: "npm"
26+
27+
- name: Install dependencies
28+
run: npm ci
2529

2630
- name: Check Types
2731
run: npm run type-check
@@ -42,14 +46,17 @@ jobs:
4246
uses: actions/setup-node@v4
4347
with:
4448
node-version: ${{ matrix.node }}
45-
cache: 'npm'
49+
cache: "npm"
50+
51+
- name: Install dependencies
52+
run: npm ci
4653

4754
- name: Test
4855
run: npm run test
4956

50-
- name: Upload Code Coverage Results
51-
uses: codecov/codecov-action@v3
57+
- name: Upload code coverage
58+
uses: actions/upload-artifact@v4
59+
if: matrix.node == '21'
5260
with:
53-
file: ./coverage/coverage-final.json
54-
fail_ci_if_error: true
55-
verbose: true
61+
name: coverage
62+
path: ./coverage

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ out
8686

8787
# Nuxt.js build / generate output
8888
.nuxt
89-
dist
9089

9190
# Gatsby files
9291
.cache/
@@ -111,6 +110,3 @@ dist
111110

112111
# Stores VSCode versions used for testing VSCode extensions
113112
.vscode-test
114-
115-
yarn.lock
116-
package-lock.json

.husky/pre-commit

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
npm run test
2+
npm run build

dist/index.cjs

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
"use strict";
2+
var __defProp = Object.defineProperty;
3+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4+
var __getOwnPropNames = Object.getOwnPropertyNames;
5+
var __hasOwnProp = Object.prototype.hasOwnProperty;
6+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8+
var __export = (target, all) => {
9+
for (var name in all)
10+
__defProp(target, name, { get: all[name], enumerable: true });
11+
};
12+
var __copyProps = (to, from, except, desc) => {
13+
if (from && typeof from === "object" || typeof from === "function") {
14+
for (let key of __getOwnPropNames(from))
15+
if (!__hasOwnProp.call(to, key) && key !== except)
16+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17+
}
18+
return to;
19+
};
20+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21+
var __publicField = (obj, key, value) => {
22+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
23+
return value;
24+
};
25+
26+
// src/index.ts
27+
var src_exports = {};
28+
__export(src_exports, {
29+
RouteBuilder: () => RouteBuilder,
30+
RouteMiddleware: () => RouteMiddleware
31+
});
32+
module.exports = __toCommonJS(src_exports);
33+
34+
// src/route-builder.ts
35+
var RouteBuilder = class {
36+
constructor(middleware, routes) {
37+
__publicField(this, "_middleware", []);
38+
__publicField(this, "_excludeList", []);
39+
__publicField(this, "_includeList", []);
40+
__publicField(this, "_routes");
41+
this._middleware = middleware;
42+
this._routes = routes;
43+
}
44+
/**
45+
* Constructs in instance of RouteBuilder with the configured middleware and routes.
46+
* @param middleware list of middleware this RouteBuilder instance will use
47+
* @param routes list of routes this RouteBuilder instance will use
48+
*/
49+
static from(middleware, routes) {
50+
return new RouteBuilder(middleware, routes);
51+
}
52+
/**
53+
* Main builder function that wraps the handlers and returns an object that
54+
* can be exported. For example
55+
* ```ts
56+
* export const {
57+
* GET, POST, DELETE
58+
* } = RouteBuilder
59+
* .from(middleware, routes)
60+
* .build();
61+
* ```
62+
* @param routes an object with keys the HTTP methods and values the route
63+
* handlers implementing the associated API functionality.
64+
*/
65+
build(routes) {
66+
routes = routes || this._routes;
67+
const handlers = {};
68+
Object.entries(routes).forEach(([method, handler]) => {
69+
handlers[method] = this._compose(method, handler);
70+
});
71+
return handlers;
72+
}
73+
/**
74+
* Enables middleware to be included if they are excluded by default for
75+
* the current routes being built.
76+
* @param middlewareList list of middleware by name to use while wrapping
77+
* the routes
78+
*/
79+
include(...middlewareList) {
80+
this._includeList = this._includeList.concat(middlewareList);
81+
return this;
82+
}
83+
/**
84+
* Excludes a list of middleware by name for a the current set of routes if
85+
* they are included by default.
86+
* @param middlwarelist list of middleware names to exclude from the routes
87+
* being built
88+
*/
89+
exclude(...middlwarelist) {
90+
this._excludeList = this._excludeList.concat(middlwarelist);
91+
return this;
92+
}
93+
/**
94+
* Internal function that returns an array of middleware the current routes
95+
* being built will use.
96+
* @param method HTTP method for the handler the middleware is wrapping
97+
*/
98+
_getRouteMiddleware(method) {
99+
return this._middleware.filter((config) => {
100+
if (!config.methods.includes(method)) {
101+
return false;
102+
}
103+
if (config.default.include) {
104+
return !this._excludeList.includes(config.name);
105+
} else if (config.default.exclude) {
106+
return this._includeList.includes(config.name);
107+
}
108+
}).map((m) => {
109+
return {
110+
name: m.name,
111+
function: m.middleware
112+
};
113+
});
114+
}
115+
/**
116+
* Internal function that composes the associated middleware together,
117+
* wrapping the original route handler.
118+
* @param method HTTP method for the API handler
119+
* @param handler API handler being wrapped
120+
*/
121+
_compose(method, handler) {
122+
const middleware = this._getRouteMiddleware(method);
123+
const wrapper = /* @__PURE__ */ __name(async (i, request, context) => {
124+
if (i === middleware.length) {
125+
return await handler(request, context);
126+
}
127+
const nextFunction = /* @__PURE__ */ __name(async () => {
128+
return await wrapper(i + 1, request, context);
129+
}, "nextFunction");
130+
return middleware[i].function(request, nextFunction, context, handler);
131+
}, "wrapper");
132+
return async (request, context) => {
133+
return wrapper(0, request, context);
134+
};
135+
}
136+
};
137+
__name(RouteBuilder, "RouteBuilder");
138+
139+
// src/constants.ts
140+
var DEFAULT_HTTP_METHODS = [
141+
"GET",
142+
"POST",
143+
"PUT",
144+
"PATCH",
145+
"DELETE",
146+
"OPTIONS",
147+
"HEAD"
148+
];
149+
150+
// src/route-middleware.ts
151+
var RouteMiddleware = class {
152+
constructor(middleware) {
153+
__publicField(this, "middleware");
154+
this.middleware = middleware;
155+
}
156+
/**
157+
* Constructs a RouteMiddleware instance from an array of middleware config
158+
* objects and middleware functions.
159+
* @param middleware list of middleware functions and middleware config objects
160+
*/
161+
static from(...middleware) {
162+
const parsedMiddleware = middleware.map((m) => {
163+
if (typeof m === "function") {
164+
return {
165+
name: m.name,
166+
middleware: m,
167+
default: {
168+
include: true
169+
},
170+
methods: DEFAULT_HTTP_METHODS
171+
};
172+
} else {
173+
let methods = Array.from(new Set(m.methods));
174+
m.name = m.name || m.middleware.name;
175+
m.methods = methods.length > 0 ? methods : DEFAULT_HTTP_METHODS;
176+
m.default = m.default || {
177+
include: true
178+
};
179+
return m;
180+
}
181+
});
182+
return new RouteMiddleware(parsedMiddleware);
183+
}
184+
/**
185+
* Merges a list of RouteMiddleware instances to combine their middleware by
186+
* concatenation.
187+
* @param routeMiddleware
188+
*/
189+
static merge(...routeMiddleware) {
190+
return new RouteMiddleware([]).merge(...routeMiddleware);
191+
}
192+
/**
193+
* Concatenates the middleware from a list of RouteMiddleware instances into
194+
* the current list of middleware
195+
* @param routeMiddleware
196+
*/
197+
merge(...routeMiddleware) {
198+
this.middleware = this.middleware.concat(...routeMiddleware.map((m) => m.middleware));
199+
return this;
200+
}
201+
/**
202+
* Returns an instance of RouteBuilder containing the middleware configured
203+
* in this instance and the routes this middleware should be applied to.
204+
* @param routes object containing routes this RouteMiddleware instance
205+
* will wrap
206+
*/
207+
routes(routes) {
208+
if (routes.hasOwnProperty("prototype")) {
209+
routes = new routes();
210+
}
211+
return RouteBuilder.from(this.middleware, routes);
212+
}
213+
};
214+
__name(RouteMiddleware, "RouteMiddleware");
215+
// Annotate the CommonJS export names for ESM import in node:
216+
0 && (module.exports = {
217+
RouteBuilder,
218+
RouteMiddleware
219+
});

0 commit comments

Comments
 (0)