
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
@ceski23/stan-js-utils
Advanced tools
This package is UNOFFICIAL set of utils for use with [stan-js](https://github.com/codemask-labs/stan-js).
This package is UNOFFICIAL set of utils for use with stan-js.
This is small util that helps to maintain backward compatibility of state when persisting it e.g. in localStorage
. It accepts one parameter - configuration object with few params:
version
- this is current version of your state, you should increase it each time you change your state in non-backwards compatible waymigrations
- object with state migrations, each key is version number and value is function which will get old state and should return migrated stateserialize
/deserialize
- custom (de)serializer, same as in stan-jsLet's say you stored user info in store and persisted it in localStorage
:
type UserData = {
username: string
}
const store = createStore({
user: storage<UserData | null>(null, {
...withMigrations({
version: 0,
migrations: {},
}),
}),
})
Then, after some time you decide that you also want to have user's name in that state. You now have to do two steps:
const userDataV0Schema = z.object({
username: z.string(),
})
type UserData = {
username: string
name: string
}
const store = createStore({
user: storage<UserData | null>(null, {
...withMigrations({
version: 1,
migrations: {
1: (prev: unknown) => {
const { success, data } = userDataV0Schema.safeParse(prev)
return success ? { ...data, name: data.username } : null
},
},
}),
}),
})
In above example we declare migration from version 0 to 1 which is using zod
to parse data and add new property. In the future if you change state's shape again you will have to repeat those steps.
Let's add age
property:
const userDataV0Schema = z.object({
username: z.string(),
})
const userDataV1Schema = userDataV0Schema.extend({
name: z.string(),
})
type UserData = {
username: string
name: string
}
const store = createStore({
user: storage<UserData | null>(null, {
...withMigrations({
version: 2,
migrations: {
1: (prev: unknown) => {
const { success, data } = userDataV0Schema.safeParse(prev)
return success ? { ...data, name: data.username } : null
},
2: (prev: unknown) => {
const { success, data } = userDataV1Schema.safeParse(prev)
return success ? { ...data, age: 0 } : null
},
},
}),
}),
})
FAQs
This package is UNOFFICIAL set of utils for use with [stan-js](https://github.com/codemask-labs/stan-js).
We found that @ceski23/stan-js-utils demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.