vaderjs 1.4.2-yml56 → 1.4.3

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,235 +0,0 @@
1
- /**
2
- * @fileoverview - A simple router for vaderjs - Kuai
3
- * @version - 1.0.0
4
- */
5
- export class Kuai{
6
- constructor(config = { container: '#app'}){
7
- this.routes = [];
8
- this.middleware = [];
9
- this.container = config.container ? config.container : document.getElementById('app');
10
- this.renderPlugins = [];
11
- }
12
-
13
- res = {
14
- /**
15
- * @description render text to the container
16
- * @param {string} data
17
- */
18
- text: (data) => {
19
- this.container.innerHTML = data;
20
- },
21
- /**
22
- * @method html
23
- * @description render html to the container
24
- * @param {any} data
25
- * @returns
26
- */
27
- html: (data) => {
28
- switch(typeof data){
29
- case 'function':
30
- if(this.renderPlugins.length > 0){
31
- this.renderPlugins.forEach((plugin) => {
32
- if(plugin.for === 'html'){
33
- console.log('Plugin', plugin)
34
- plugin.plugin(data, this.container)
35
- }
36
- })
37
- return;
38
- }
39
- this.container.innerHTML = data();
40
- break;
41
- case 'string':
42
- this.container.innerHTML = data;
43
- break;
44
- }
45
-
46
- },
47
- /**
48
- * @method json
49
- * @description render json to the container
50
- * @param {Object} data
51
- */
52
- json: (data) => {
53
- this.container.innerHTML = `<Oject data=${JSON.stringify(data)}></Object>`
54
- }
55
- }
56
- req = {
57
- /**
58
- * @method navigate
59
- * @description - navigate to a new route
60
- * @param {string} path
61
- */
62
- navigate: (path) => {
63
- window.history.pushState({}, '', path);
64
- let currentPath = this.match(window.location.pathname.replace('/index.html', ''))
65
- if(currentPath){
66
- this.currentRoute = currentPath.path
67
- currentPath.callback(this.res, currentPath.params, this.extractQueryParams(window.location.search));
68
- }
69
- },
70
- /**
71
- * @method back
72
- * @description - go back to the previous route
73
- */
74
- back: () => {
75
- window.history.back();
76
- },
77
-
78
- /**
79
- * @method forward
80
- * @description - go forward to the next route
81
- * @returns {void}
82
- * **/
83
- forward: () => {
84
- window.history.forward();
85
- },
86
- url: window.location
87
- }
88
- /**
89
- * @private
90
- */
91
- extractQueryParams(path){
92
- let params = new URLSearchParams(path);
93
- let query = {};
94
- for(let param of params){
95
- query[param[0]] = param[1];
96
- }
97
- return query;
98
- }
99
- /**
100
- * @private
101
- */
102
- extractParams(routePath, currentPath){
103
- const routeParts = routePath.split('/').filter((part) => part !== '');
104
- const hashParts = currentPath.split('/').filter((part) => part !== '');
105
- const params = {};
106
- routeParts.forEach((part, index) => {
107
- if (part.startsWith(':')) {
108
- const paramName = part.slice(1);
109
- params[paramName] = hashParts[index];
110
- }else if(part.startsWith('*')){
111
- let array = hashParts.slice(index)
112
- array.forEach((i, index)=>{
113
- params[index] = i
114
- })
115
- };
116
- });
117
- return params;
118
- }
119
- use(path, middleware){
120
- this.middleware.push({path, middleware});
121
- }
122
- /**
123
- * @method usePlugin
124
- * @description - add a plugin to handle how the route should be rendered
125
- * @param {Function} plugin
126
- * @param {('html')} method
127
- */
128
- usePlugin(plugin, method){
129
- this.renderPlugins.push({plugin, for: method})
130
- }
131
- /**
132
- * @method match
133
- * @description - match a route to the current path and return the route object
134
- * @param {string} route
135
- * @returns {Object} - {path: string, callback: Function, params: Object}
136
- */
137
- match(hash){
138
- hash = hash.endsWith('/') ? hash.slice(0, -1) : hash;
139
- hash.includes('index.html') ? hash = hash.replace('index.html', '') : null;
140
- if(hash.includes('?')){
141
- hash = hash.split('?')[0]
142
- }
143
- let route = this.routes.find((route) => {
144
- if (route.path === hash) {
145
- return true;
146
- }
147
-
148
- if(hash === '' && route.path === '/'){
149
- return true
150
- }
151
-
152
-
153
- if (route.path.includes('*') || route.path.includes(':')) {
154
- const routeParts = route.path.split('/').filter((part) => part !== '');
155
- const hashParts = hash.split('/').filter((part) => part !== '');
156
- if(this.basePath){
157
- hashParts.shift();
158
- }
159
- if (routeParts.length !== hashParts.length && !route.path.endsWith('*')) {
160
- return false;
161
- }
162
-
163
- for (let index = 0; index < routeParts.length; index++) {
164
- const routePart = routeParts[index];
165
- const hashPart = hashParts[index];
166
-
167
-
168
- if (routePart.startsWith(':') || routePart.startsWith('*')) {
169
-
170
- continue;
171
- }
172
-
173
- if (routePart !== hashPart) {
174
- return false;
175
- }
176
- }
177
-
178
-
179
- return true;
180
- }
181
-
182
- });
183
- if(route){
184
- let params = this.extractParams(route.path, hash)
185
- return { ...route, params}
186
- }
187
- return null;
188
-
189
- }
190
-
191
- /**
192
- * @description - create a new route
193
- * @param {string} path
194
- * @param {Function} callback
195
- */
196
- get(path, callback){
197
- this.routes.push({path, callback});
198
- }
199
-
200
- /**
201
- * @method listen
202
- * @description - listen for route changes
203
- */
204
- listen(){
205
- let currentPath = this.match(window.location.pathname.replace('/index.html', ''))
206
- if(currentPath){
207
- this.middleware.forEach((middleware) => {
208
- if(middleware.path === currentPath.path){
209
- middleware.middleware();
210
- }
211
- });
212
- this.currentRoute = currentPath.path
213
- let obj = {
214
- ...this.res,
215
- res: this.res,
216
- req:{
217
- ...this.req,
218
- params: (param) => currentPath.params[param]
219
-
220
- }
221
- }
222
- currentPath.callback(obj);
223
- }
224
- window.onpopstate = () => {
225
- let currentPath = this.match(window.location.pathname.replace('/index.html', ''))
226
- if(currentPath){
227
- this.currentRoute = currentPath.path
228
- currentPath.callback(this.res, currentPath.params, this.extractQueryParams(window.location.search));
229
- }
230
- }
231
-
232
- }
233
- }
234
-
235
- export default Kuai;
@@ -1,98 +0,0 @@
1
- import { Glob } from 'bun'
2
- import fs from 'fs'
3
- const glob = new Glob("/**/*.{ts,tsx,js,jsx}", {
4
- absolute: true,
5
- });
6
- /**
7
- * @description This function generates cloudflare functions from the routes folder
8
- */
9
- async function generate(){
10
- let config = await import(process.cwd() + '/vader.config.js').then((config) => { return config.default })
11
- let start = Date.now()
12
- for(var i of glob.scanSync({cwd: process.cwd() + '/routes', absolute: true})){
13
- let data = await Bun.file(i).text()
14
- let method = ''
15
- i = i.replaceAll('\\', '/').replace(process.cwd(), '')
16
- if(!data.includes(data.match(new RegExp('export\\s+default')))){
17
- throw new Error('File must have a default export')
18
- }
19
- data.split('\n').forEach((line, index) => {
20
- if(line.includes('GET') || line.includes('POST') || line.includes('PUT') || line.includes('DELETE') && line.includes('function')){
21
- line = line.replace(/export\s+default\s+async\s+function/g, line.includes('async') ? 'async function' : 'function')
22
- method = line.split('function')[1].split('(')[0].trim()
23
- data = data.replace(data.split('\n')[index], line)
24
- data = data + `\nexport async function onRequest${method.toLowerCase().charAt(0).toUpperCase() + method.toLowerCase().slice(1)}(request){
25
- let req = {
26
- url: request.request.url,
27
- method: request.request.method,
28
- headers: request.request.headers,
29
- }
30
- let res = {
31
- params: (param) => {
32
- return request.params[param]
33
- },
34
- query: (param) => {
35
- let url = new URL(request.request.url)
36
- return url.searchParams.get(param)
37
- }
38
- }
39
- let response = await ${method}({req, res})
40
- return response
41
- }
42
- `
43
- }
44
- });
45
-
46
- let env = `globalThis.env = {
47
-
48
- ${
49
- Object.keys(config?.env).map((key) => {
50
- return `${key}:"${config.env[key]}",`
51
- } ).join('\n')
52
- }
53
- ${Object.keys(process.env).map((key) => {
54
- let value = process.env[key].replace(/"/g, '\\"')
55
- value = value.replace(/\\n/g, '\\n') // remove new lines
56
- value = value.replace(/\\r/g, '\\r') // remove carriage returns
57
- value = value.replace(/\\t/g, '\\t') // remove tabs
58
- value = value.replace('/\/g', '\\\\') // remove octal escape sequences
59
- // remove octal escape sequences
60
- value = value.replace(/\\[0-7]{1,3}/g, '');
61
- return `${key}:"${value}"`
62
- } ).join(',\n')
63
- }
64
- };
65
- `
66
- // make env all on one line
67
- env = env.replace(/\n/g, '')
68
- data = env + data
69
-
70
-
71
- fs.mkdirSync(process.cwd() + '/build/functions' + i.split('/routes')[1].split('/').slice(0, -1).join('/'), { recursive: true })
72
- fs.writeFileSync(process.cwd() + '/build/functions/' + i.split('/routes')[1].replace('.ts', '.js').replace('.tsx', '.js').replace('.jsx', '.js'), data)
73
-
74
- let nodeModules = process.cwd() + '/node_modules/vaderjs/plugins/cloudflare/toCopy'
75
- let glb = new Glob('**/*', {
76
- absolute: true,
77
- cwd: nodeModules
78
- })
79
- for(var file of glb.scanSync({cwd: nodeModules, absolute: true})){
80
- file = file.replaceAll('\\', '/').replace(nodeModules, '')
81
- if(fs.existsSync('build/' + file.split('/toCopy')[1])) continue
82
- let data = await Bun.file(file).text()
83
- let path = file.split('/toCopy')[1]
84
- Bun.write(process.cwd() + '/build/' + path, data)
85
- }
86
- }
87
- console.log(`\x1b[32msuccess \x1b[0m - Cloudflare Functions Compiled in: ${Date.now() - start}ms`)
88
- }
89
- export default {
90
- name: 'Cloudflare Functions Plugin',
91
- description: 'This plugin utilizes cloudflare functios for server side rendering',
92
- version: '0.0.1',
93
- type: "SSR",
94
- init: async (path, options) => {
95
- console.log(`\x1b[32mevent \x1b[0m - Cloudflare Functions Plugin Initialized`)
96
- await generate()
97
- }
98
- }