You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Remove Typed Session Storage and dependency on Zod v3 (#530)
- Removes the deprecated Typed Session utils.
- Removes the dependency on Zod for Sec-Fetch related utils, they now
use plain JS to validate the header values.
- This also removes the dependency on Zod as only Typed Cookie could use
it but it depends on Standard Schema instead so any other lib can work
> This depends on `@standard-schema/spec`, and React Router.
1138
1137
1139
-
Cookie objects in Remix allows any type, the typed cookies from Remix Utils lets you use Zod to parse the cookie values and ensure they conform to a schema.
1138
+
Cookie objects in Remix allows any type, the typed cookies from Remix Utils lets you use any Standard Schema compatible library to parse the cookie values and ensure they conform to a schema.
1140
1139
1141
1140
```ts
1142
1141
import { createCookie } from"react-router";
@@ -1166,7 +1165,7 @@ You could also use typed cookies with any sessionStorage mechanism from Remix.
1166
1165
1167
1166
```ts
1168
1167
let cookie =createCookie("session", cookieOptions);
1169
-
let schema =z.object({ token: z.string() }).nullable();
1168
+
let schema =z.object({ token: z.string().nullish() }).nullable();
> This util is marked as deprecated and will be removed in the next major version. Use the generic accepted by React Router's `createSessionStorage` helpers instead.
1233
-
1234
-
> [!NOTE]
1235
-
> This depends on `zod`, and React Router.
1236
-
1237
-
Session objects in Remix allows any type, the typed sessions from Remix Utils lets you use Zod to parse the session data and ensure they conform to a schema.
// you can use a Remix's Cookie container or a Remix Utils' Typed Cookie container
1250
-
let sessionStorage =createCookieSessionStorage({ cookie });
1251
-
1252
-
// pass the session storage and the schema
1253
-
let typedSessionStorage =createTypedSessionStorage({ sessionStorage, schema });
1254
-
```
1255
-
1256
-
Now you can use typedSessionStorage as a drop-in replacement for your normal sessionStorage.
1257
-
1258
-
```ts
1259
-
let session =typedSessionStorage.getSession(request.headers.get("Cookie"));
1260
-
1261
-
session.get("token"); // this will be a string or undefined
1262
-
session.get("count"); // this will be a number
1263
-
session.get("random"); // this will make TS yell because it's not in the schema
1264
-
1265
-
session.has("token"); // this will be a boolean
1266
-
session.has("count"); // this will be a boolean
1267
-
1268
-
// this will make TS yell because it's not a string, if you ignore it it will
1269
-
// throw a ZodError
1270
-
session.set("token", 123);
1271
-
```
1272
-
1273
-
Now Zod will ensure the data you try to save to the session is valid by not allowing you to get, set or unset data.
1274
-
1275
-
> [!TIP]
1276
-
> Remember that you either need to mark fields as optional or set a default value in the schema, otherwise it will be impossible to call getSession to get a new session object.
1277
-
1278
-
You can also use async refinements in your schemas because typed sesions uses parseAsync method from Zod.
1279
-
1280
-
```ts
1281
-
let schema =z.object({
1282
-
token: z
1283
-
.string()
1284
-
.optional()
1285
-
.refine(async (token) => {
1286
-
if (!token) returntrue; // handle optionallity
1287
-
let user =awaitgetUserByToken(token);
1288
-
returnuser!==null;
1289
-
}, "INVALID_TOKEN"),
1290
-
});
1291
-
1292
-
let typedSessionStorage =createTypedSessionStorage({ sessionStorage, schema });
1293
-
1294
-
// this will throw if the token stored in the session is not valid anymore
0 commit comments