@jon49/sw 0.12.14 → 0.13.1

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.
Files changed (2) hide show
  1. package/lib/routes.ts +25 -8
  2. package/package.json +1 -1
package/lib/routes.ts CHANGED
@@ -1,6 +1,6 @@
1
- let { html, links } =
1
+ let { html, links, db, globalDb } =
2
2
  // @ts-ignore
3
- self.app as { links: { file: string, url: string }[], html: Function }
3
+ self.app as { links: { file: string, url: string }[], html: Function, db: any, globalDb: any }
4
4
 
5
5
  if (!links) {
6
6
  console.error("Expecting links defined with `self.app.links`, but found none.")
@@ -19,8 +19,7 @@ const searchParamsHandler = {
19
19
  }
20
20
  }
21
21
 
22
- function searchParams<TReturn>(req: Request) : TReturn & {_url: URL} {
23
- let url = new URL(req.url)
22
+ function searchParams<TReturn>(url: URL) : TReturn & {_url: URL} {
24
23
  return new Proxy(url, searchParamsHandler)
25
24
  }
26
25
 
@@ -99,6 +98,7 @@ export async function findRoute(url: URL, method: unknown) {
99
98
  }
100
99
  // @ts-ignore
101
100
  for (const r of self.app.routes) {
101
+ // @ts-ignore
102
102
  if (r.file
103
103
  && (r.route instanceof RegExp && r.route.test(url.pathname)
104
104
  || (r.route instanceof Function && r.route(url)))) {
@@ -144,7 +144,25 @@ interface ExectuteHandlerOptions {
144
144
  async function executeHandler({ url, req, event }: ExectuteHandlerOptions) : Promise<Response> {
145
145
  let method = req.method.toLowerCase()
146
146
  let isPost = method === "post"
147
- if (!isPost && !url.pathname.endsWith("/")) return cacheResponse(url.pathname, event)
147
+ if (!isPost) {
148
+ if (!url.pathname.endsWith("/")) {
149
+ return cacheResponse(url.pathname, event)
150
+ }
151
+
152
+ if (url.searchParams.get("login") === "success") {
153
+ await globalDb.setLoggedIn(true)
154
+ }
155
+
156
+ let lastUrl: string | undefined
157
+ if (url.pathname === "/web/" && (lastUrl = (await db.get("last-url"))?.url)) {
158
+ url = new URL(lastUrl)
159
+ } else if (url.searchParams.has("pushUrl") || url.searchParams.has("hz")) {
160
+ let saveUrl = new URL(url.href)
161
+ saveUrl.searchParams.delete("pushUrl")
162
+ saveUrl.searchParams.delete("hz")
163
+ await db.set("last-url", { url: saveUrl.href }, { sync: false })
164
+ }
165
+ }
148
166
 
149
167
  let handlers =
150
168
  <RouteHandler<RouteGetArgs | RoutePostArgs> | null>
@@ -155,7 +173,7 @@ async function executeHandler({ url, req, event }: ExectuteHandlerOptions) : Pro
155
173
  try {
156
174
  let messages: string[] = []
157
175
  const data = await getData(req)
158
- let query = searchParams<{ handler?: string }>(req)
176
+ let query = searchParams<{ handler?: string }>(url)
159
177
  let args = { req, data, query }
160
178
  let result = await (
161
179
  handlers instanceof Function
@@ -175,7 +193,7 @@ async function executeHandler({ url, req, event }: ExectuteHandlerOptions) : Pro
175
193
  }
176
194
 
177
195
  if (isHtml(result)) {
178
- if (req.url.includes("hz")) {
196
+ if (url.searchParams.has("hz")) {
179
197
  result = html`<template>${result}</template>`
180
198
  }
181
199
 
@@ -368,4 +386,3 @@ type RequireAtLeastOne<T, Keys extends keyof T = keyof T> =
368
386
  export type Route = RequireAtLeastOne<Route_, "file" | "get" | "post">
369
387
  export type RoutePage = Pick<Route_, "get" | "post">
370
388
 
371
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jon49/sw",
3
- "version": "0.12.14",
3
+ "version": "0.13.1",
4
4
  "description": "Packages for MVC service workers.",
5
5
  "type": "module",
6
6
  "files": [