Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify some types and restructure utilities #3

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"error",
{ "devDependencies": ["**/*.spec.ts", "vite.config.ts"] },
],
"import/extensions": ["error", "never"],
"no-console": "error",
},
"ignorePatterns": ["dist", "scripts", "fixtures/**/*.js", "fixtures/**/*.cjs"],
"ignorePatterns": ["dist", "scripts", "fixtures/**/*.js", "fixtures/**/*.cjs", "coverage"],
}
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/CHANGELOG.md
pnpm-lock.yaml
fixtures/**/output/**/*
fixtures/**/output/**/*
coverage
2 changes: 1 addition & 1 deletion fixtures/check-route-match/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VercelConfig } from '../../src/types';
import type { VercelConfig } from '@/types/vercel-config';

export const config: VercelConfig = {
version: 3,
Expand Down
3 changes: 2 additions & 1 deletion fixtures/check-route-match/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BuildOutput } from '../../src/types';
import type { BuildOutput } from '@/types/build-output';

import { functionAsset, htmlAsset } from '../run-test-set';

export const fileSystem: BuildOutput = {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/dynamic-routes/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VercelConfig } from '../../src/types';
import type { VercelConfig } from '@/types/vercel-config';

export const config: VercelConfig = {
version: 3,
Expand Down
3 changes: 2 additions & 1 deletion fixtures/dynamic-routes/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BuildOutput } from '../../src/types';
import type { BuildOutput } from '@/types/build-output';

import { functionAsset, htmlAsset, staticAsset } from '../run-test-set';

export const fileSystem: BuildOutput = {
Expand Down
2 changes: 1 addition & 1 deletion fixtures/i18n/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VercelConfig } from '../../src/types';
import type { VercelConfig } from '@/types/vercel-config';

export const config: VercelConfig = {
version: 3,
Expand Down
3 changes: 2 additions & 1 deletion fixtures/i18n/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BuildOutput } from '../../src/types';
import type { BuildOutput } from '@/types/build-output';

import { functionAsset, htmlAsset, staticAsset } from '../run-test-set';

export const staticLocales = ['en', 'fr', 'nl', 'es'] as const;
Expand Down
2 changes: 1 addition & 1 deletion fixtures/infinite-loop/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VercelConfig } from '../../src/types';
import type { VercelConfig } from '@/types/vercel-config';

export const config: VercelConfig = {
version: 3,
Expand Down
3 changes: 2 additions & 1 deletion fixtures/infinite-loop/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BuildOutput } from '../../src/types';
import type { BuildOutput } from '@/types/build-output';

import { functionAsset, htmlAsset } from '../run-test-set';

export const fileSystem: BuildOutput = {
Expand Down
21 changes: 6 additions & 15 deletions fixtures/run-test-set.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,16 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';

import {
applyHeaders,
applySearchParams,
collectLocalesFromRoutes,
createRouteRequest,
} from 'src/utils';
// eslint-disable-next-line import/no-extraneous-dependencies
import { expect, suite, test, vi } from 'vitest';

import type { BuildOutputItem, EdgeFunction, ExecutionContext } from '@/types/build-output';
import type { Assets, Fetcher } from '@/types/request-context';
import type { VercelConfig } from '@/types/vercel-config';
import { applyHeaders, applySearchParams, createRouteRequest } from '@/utils/http';
import { collectLocalesFromRoutes, groupRoutesByPhase } from '@/utils/routing';

import { Router } from '../src';
import type {
Assets,
BuildOutputItem,
EdgeFunction,
ExecutionContext,
Fetcher,
VercelConfig,
} from '../src/types';
import { groupRoutesByPhase } from '../src/utils';

type TestCase = {
name: string;
Expand Down
2 changes: 1 addition & 1 deletion fixtures/trailing-slash/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VercelConfig } from '../../src/types';
import type { VercelConfig } from '@/types/vercel-config';

export const config: VercelConfig = {
version: 3,
Expand Down
3 changes: 2 additions & 1 deletion fixtures/trailing-slash/file-system.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { BuildOutput } from '../../src/types';
import type { BuildOutput } from '@/types/build-output';

import { functionAsset, htmlAsset, staticAsset } from '../run-test-set';

export const fileSystem: BuildOutput = {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"typescript": "^5.6.2",
"vite": "^5.4.8",
"vite-plugin-dts": "^4.2.2",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "^2.1.1"
},
"dependencies": {
Expand Down
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { RoutesGroupedByPhase } from '@/types/build-output';
import type { RequestContext } from '@/types/request-context';
import { applyHeaders, applySearchParams, isUrl } from '@/utils/http';

import type { ConfigMetadata, RoutingMatch } from './routes-matcher';
import { RoutesMatcher } from './routes-matcher';
import type { ConfigMetadata, RequestContext, RoutesGroupedByPhase } from './types';
import type { RoutingMatch } from './utils';
import { applyHeaders, applySearchParams, isUrl } from './utils';

export class Router {
constructor(
Expand Down
57 changes: 36 additions & 21 deletions src/routes-matcher.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
import { parse } from 'cookie';

import type {
ConfigMetadata,
Phase,
RequestContext,
RoutesGroupedByPhase,
SourceRoute,
VercelWildCard,
} from './types';
import type { MatchPCREResult, RoutingMatch } from './utils';
import {
applyHeaders,
applyPCREMatches,
applySearchParams,
checkHasField,
getNextPhase,
isLocaleTrailingSlashRegex,
isUrl,
matchPCRE,
parseAcceptLanguage,
} from './utils';
import type { Phase, RoutesGroupedByPhase, SourceRoute } from '@/types/build-output';
import type { RequestContext } from '@/types/request-context';
import type { WildCard } from '@/types/vercel-config';
import { applyHeaders, applySearchParams, isUrl, parseAcceptLanguage } from '@/utils/http';
import type { MatchPCREResult } from '@/utils/pcre';
import { applyPCREMatches, matchPCRE } from '@/utils/pcre';
import { checkHasField, getNextPhase, isLocaleTrailingSlashRegex } from '@/utils/routing';

export type ConfigMetadata = {
locales: Set<string>;
wildcardConfig: WildCard[] | undefined;
};

export type RoutingMatch = {
path: string;
status: number | undefined;
headers: {
/**
* The headers present on a source route.
* Gets applied to the final response before the response headers from running a function.
*/
normal: Headers;
/**
* The *important* headers - the ones present on a source route that specifies `important: true`.
* Gets applied to the final response after the response headers from running a function.
*/
important: Headers;
/**
* Tracks if a location header is found, and what the value is, after running a middleware function.
*/
middlewareLocation?: string | null;
};
searchParams: URLSearchParams;
body: BodyInit | undefined | null;
};

export type CheckRouteStatus = 'skip' | 'next' | 'done' | 'error';
export type CheckPhaseStatus = Extract<CheckRouteStatus, 'error' | 'done'>;
Expand All @@ -35,7 +50,7 @@ export class RoutesMatcher {
private cookies: Record<string, string>;

/** Wildcard match from the Vercel build output config */
private wildcardMatch: VercelWildCard | undefined;
private wildcardMatch: WildCard | undefined;

/** Path for the matched route */
public path: string;
Expand Down
7 changes: 0 additions & 7 deletions src/types/assets.ts

This file was deleted.

Loading