Skip to content

Commit

Permalink
Deduplicate serialized patterns
Browse files Browse the repository at this point in the history
Enable noUncheckedIndexedAccess and apply fixes
  • Loading branch information
jollytoad committed Mar 14, 2024
1 parent b6cb923 commit ee8a07e
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 7 deletions.
6 changes: 4 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "@http/fns",
"version": "0.6.3",
"version": "0.6.4",
"description": "HTTP server functions (including routing)",
"tasks": {
"example": "deno run -A --watch",
"test": "deno test -A --location=http://localhost:8000 --no-check",
"check": "deno fmt && deno lint && deno check **/*.ts && deno task exports",
"exports": "deno run --allow-read=. --allow-write=deno.json scripts/exports.ts"
"exports": "deno run --allow-read=. --allow-write=deno.json scripts/exports.ts",
"lock": "rm -f deno.lock && deno check **/*.ts"
},
"compilerOptions": {
"noUncheckedIndexedAccess": true,
"verbatimModuleSyntax": true
},
"imports": {
Expand Down
3 changes: 2 additions & 1 deletion lib/as_serializable_pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export function asSerializablePattern(
pattern: RoutePattern,
): SerializableRoutePattern {
if (Array.isArray(pattern)) {
return pattern.flatMap(asSerializablePattern);
const array = [...new Set(pattern.flatMap(asSerializablePattern))];
return array.length === 1 && array[0] ? array[0] : array;
} else if (typeof pattern === "string") {
return pattern;
} else {
Expand Down
6 changes: 3 additions & 3 deletions lib/by_media_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function byMediaType<A extends unknown[]>(
| undefined;

return mediaType && handlers[mediaType]
? handlers[mediaType](req, ...args)
? handlers[mediaType]!(req, ...args)
: fallbackExt(req, ...args);
} else {
// Return the appropriate media type according to the Accept header
Expand All @@ -52,8 +52,8 @@ export function byMediaType<A extends unknown[]>(
| MediaType
| undefined;

let response = await (mediaType
? handlers[mediaType](req, ...args)
let response = await (mediaType && handlers[mediaType]
? handlers[mediaType]!(req, ...args)
: fallbackAccept(req, ...args));

if (response) {
Expand Down
2 changes: 1 addition & 1 deletion lib/discover_routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ function consolidateRoutes(routes: ComparableRoute[]): DiscoveredRoute[] {
function nextRoute(module?: string) {
if (currentModule && consolidatedPattern.length > 0) {
consolidatedRoutes.push({
pattern: consolidatedPattern.length === 1
pattern: consolidatedPattern.length === 1 && consolidatedPattern[0]
? consolidatedPattern[0]
: consolidatedPattern,
module: asModule(currentModule),
Expand Down

0 comments on commit ee8a07e

Please sign in to comment.