Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
- `notFound` removed
- `CookieStore` now has subscribe method
  • Loading branch information
krutoo committed Aug 9, 2023
1 parent a8195d0 commit 594a83f
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Handler, Enhancer, Middleware } from './types';
* @param enhance Enhancer.
* @return Enhanced fetch function.
* @todo Need to get isomorphic fetch interface.
* @todo Result type should be typeof fetch extended by enhancer (need update enhance mechanism).
* @todo Result type should be typeof fetch extended by enhancer? (need update enhance mechanism).
*/
export function configureFetch<T extends typeof fetch>(
fetchFn: T,
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type { Handler, Enhancer, Middleware, CookieStore } from './types';
export { configureFetch, applyMiddleware } from './configure';
export { html, json, notFound } from './response';
export { html, json } from './response';
export { router, route } from './server';
export { createCookieStore } from './utils';
4 changes: 0 additions & 4 deletions src/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@ export function json(...[body, options]: ConstructorParameters<typeof Response>)

return new Response(body, { ...options, headers });
}

export function notFound() {
return new Response('Not found', { status: 404 });
}
3 changes: 1 addition & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Handler } from './types';
import { notFound } from './response';

interface Route {
is(url: URL): boolean;
Expand All @@ -16,7 +15,7 @@ export function router(...routes: Route[]): Handler {
}
}

return notFound();
return new Response('Not found', { status: 404 });
};
}

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export interface Middleware {
export interface CookieStore {
getCookies(): string;
setCookie(cookie: string): void;
subscribe(listener: VoidFunction): VoidFunction;
}
8 changes: 7 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { CookieStore } from './types';
*/
export function createCookieStore(initialCookies?: string): CookieStore {
const items: Map<string, string> = new Map();
const listeners = new Set<VoidFunction>();

const setCookie = (cookie: string) => {
// IMPORTANT: separating cookie from its attributes
Expand Down Expand Up @@ -37,9 +38,14 @@ export function createCookieStore(initialCookies?: string): CookieStore {
return list.join('; ');
};

const subscribe = (listener: VoidFunction): VoidFunction => {
listeners.add(listener);
return () => listeners.delete(listener);
};

if (initialCookies && initialCookies.length > 0) {
initialCookies.split('; ').forEach(setCookie);
}

return { setCookie, getCookies };
return { setCookie, getCookies, subscribe };
}

0 comments on commit 594a83f

Please sign in to comment.