-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aca96a6
commit 3e0d272
Showing
24 changed files
with
440 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@powership/boilerplate", | ||
"version": "3.1.7", | ||
"version": "3.1.9", | ||
"author": "antoniopresto <[email protected]>", | ||
"sideEffects": false, | ||
"#type": "module", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "logstorm", | ||
"version": "3.1.7", | ||
"version": "3.1.9", | ||
"typings": "out", | ||
"author": "antoniopresto <[email protected]>", | ||
"#type": "module", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "powership", | ||
"version": "3.1.7", | ||
"version": "3.1.9", | ||
"author": "antoniopresto <[email protected]>", | ||
"#type": "module", | ||
"main": "./out/index.cjs", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "runmate", | ||
"version": "3.1.7", | ||
"version": "3.1.9", | ||
"typings": "out", | ||
"author": "antoniopresto <[email protected]>", | ||
"license": "MIT", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { createSimpleRouter } from '../createRoutes'; | ||
|
||
describe('createSimpleRouter', () => { | ||
const routes = createSimpleRouter({ | ||
home: { path: '/home' }, | ||
about: { path: '/about', query: { foo: 'int' } }, | ||
}); | ||
|
||
test('should create route handlers for each route', () => { | ||
expect(typeof routes.home.match).toBe('function'); | ||
expect(typeof routes.home.mount).toBe('function'); | ||
expect(typeof routes.about.match).toBe('function'); | ||
expect(typeof routes.about.mount).toBe('function'); | ||
}); | ||
|
||
test('mount should return correct URL without query', () => { | ||
expect(routes.home.mount()).toBe('/home'); | ||
}); | ||
|
||
test('mount should return correct URL with query', () => { | ||
expect(routes.about.mount({ query: { foo: 1 } })).toBe('/about?foo=1'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { AnyRecord, RouteMatcher, RouteUtils } from '@powership/utils'; | ||
|
||
import type { InferObjectDefinition } from './fields/Infer'; | ||
import type { ObjectDefinitionInput } from './fields/_parseFields'; | ||
|
||
export type RouteConfig = { | ||
path: string; | ||
query?: ObjectDefinitionInput; | ||
}; | ||
|
||
export type SimpleRoute = RouteConfig & { | ||
match(route: string): AnyRecord | null; | ||
mount(config?: { query: AnyRecord }): string; | ||
}; | ||
|
||
export type SimpleRouter< | ||
Routes extends Readonly<{ [K: string]: RouteConfig }> | ||
> = { | ||
[K in Extract<keyof Routes, string>]: { [K in keyof Routes]: Routes[K] } & { | ||
match: RouteMatcher<K>['match']; | ||
mount: [InferObjectDefinition<Routes[K]['query']>] extends [never] | ||
? () => string | ||
: (config: { | ||
query: InferObjectDefinition<Routes[K]['query']>; | ||
}) => string; | ||
}; | ||
}; | ||
|
||
export function createSimpleRouter< | ||
Routes extends Readonly<{ [K: string]: RouteConfig }> | ||
>(config: Routes): SimpleRouter<Routes> { | ||
const router = Object.create(null); | ||
|
||
Object.entries(config).forEach(([key, definition]) => { | ||
const { match } = RouteUtils.createRouteMatcher(definition.path); | ||
|
||
const route: SimpleRoute = { | ||
...definition, | ||
match, | ||
mount(conf?: any) { | ||
let url = '/' + RouteUtils.normalizePath(definition.path); | ||
if (conf?.query) { | ||
url += '?' + RouteUtils.stringifyQueryString(conf.query); | ||
} | ||
return url; | ||
}, | ||
}; | ||
|
||
router[key] = route; | ||
}); | ||
|
||
return router; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@powership/utils", | ||
"version": "3.1.7", | ||
"version": "3.1.9", | ||
"typings": "out", | ||
"author": "antoniopresto <[email protected]>", | ||
"license": "MIT", | ||
|
@@ -62,7 +62,9 @@ | |
"prettier": "2.8.8", | ||
"slugify": "1.6.5", | ||
"ts-toolbelt": "9.6.0", | ||
"ulid": "2.3.0" | ||
"ulid": "2.3.0", | ||
"qs": "6.11.2", | ||
"url-pattern": "1.0.3" | ||
}, | ||
"devDependencies": { | ||
"@powership/boilerplate": "workspace:*", | ||
|
@@ -72,6 +74,7 @@ | |
"@babel/preset-typescript": "7.18.6", | ||
"@powership/babel-plugins": "workspace:*", | ||
"@types/big.js": "6.1.6", | ||
"@types/qs": "6.9.10", | ||
"@types/deep-diff": "^1.0.2", | ||
"@types/ejson": "2.2.0", | ||
"@types/fs-extra": "9.0.13", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { RouteUtils } from '../routeUtils'; // Adjust the import path as necessary | ||
|
||
describe('RouteUtils', () => { | ||
describe('normalizePath', () => { | ||
it('should remove starting and trailing slashes', () => { | ||
expect(RouteUtils.normalizePath('/a/b/c/')).toBe('a/b/c'); | ||
}); | ||
|
||
it('should remove double slashes', () => { | ||
expect(RouteUtils.normalizePath('a//b//c')).toBe('a/b/c'); | ||
}); | ||
}); | ||
|
||
describe('joinPaths', () => { | ||
it('should join multiple path segments', () => { | ||
expect( | ||
RouteUtils.joinPaths('/a/', 'b', null, undefined, '', '/c', 'd/') | ||
).toBe('a/b/c/d'); | ||
}); | ||
}); | ||
|
||
describe('parseQueryString', () => { | ||
it('should parse query string into an object', () => { | ||
expect(RouteUtils.parseQueryString('?key1=value1&key2=value2')).toEqual({ | ||
key1: 'value1', | ||
key2: 'value2', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('stringifyQueryString', () => { | ||
it('should stringify an object into a query string', () => { | ||
expect( | ||
RouteUtils.stringifyQueryString({ key1: 'value1', key2: 'value2' }) | ||
).toBe('key1=value1&key2=value2'); | ||
}); | ||
}); | ||
|
||
describe('resortQueryString', () => { | ||
it('should resort query string', () => { | ||
expect(RouteUtils.resortQueryString('?b=2&a=1')).toBe('a=1&b=2'); | ||
}); | ||
}); | ||
|
||
describe('parseURL', () => { | ||
it('should parse URL into its components', () => { | ||
const parsed = RouteUtils.parseURL('/test?query=string#hash'); | ||
|
||
expect(parsed).toEqual({ | ||
pathname: '/test', | ||
search: '?query=string', | ||
hash: '#hash', | ||
route: '/test?query=string#hash', | ||
id: 'test^query=string^hash', | ||
href: 'http://localhost/test?query=string#hash', | ||
domain: 'http://localhost', | ||
isAbsolutePath: false, | ||
protocol: 'http:', | ||
host: 'localhost', | ||
hostname: 'localhost', | ||
port: '', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('isSamePathname', () => { | ||
it('should return true for URLs with the same pathname', () => { | ||
expect( | ||
RouteUtils.isSamePathname('http://localhost/a', 'http://example.com/a') | ||
).toBe(true); | ||
}); | ||
|
||
it('should return false for URLs with different pathnames', () => { | ||
expect( | ||
RouteUtils.isSamePathname('http://localhost/a', 'http://example.com/b') | ||
).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('createRouteMatcher', () => { | ||
it('should create a route matcher and match a given route', () => { | ||
const matcher = RouteUtils.createRouteMatcher('/test/:id'); | ||
expect(matcher.match('/test/123')).toEqual({ id: '123' }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.