dr-skot / json-stash

serialize and deserialize javascript objects to json
1 stars 1 forks source link

Not all functions/methods are exported (addClasses, ...) #1

Closed Oxydation closed 3 weeks ago

Oxydation commented 1 month ago

Hello,

I installed your npm package and wanted to use it for its intended purpose to save and load a state. As I have a lot of custom classes so I wanted to register them with "addClasses", but somehow a couple of methods/functions are missing from file index.d.ts.

That is the content of index.d.ts: export { toJSON as stash, fromJSON as unstash } from "./stash";

So this works fine import {stash, unstash} from "json-stash" but this does not import {addClasses, stash, unstash} from "json-stash"

Furthermore, those are also missing: addSerializers, removeSerializers, clearSerializers, getStasher

Am I missing something obvious?

Versions:

dr-skot commented 1 month ago

Let me double check the exports…

On Mon, Jul 15, 2024, at 11:14 AM, Mathias wrote:

Hello,

I installed your npm package and wanted to use it for its intended purpose to save and load a state. As I have a lot of custom classes so I wanted to register them with "addClasses", but somehow a couple of functions are missing from file index.d.ts.

That is the content of index.d.ts: export { toJSON as stash, fromJSON as unstash } from "./stash";

So this works fine import {stash, unstash} from "json-stash" but this does not import {addClasses, stash, unstash} from "json-stash"

Furthermore, those are also missing: addSerializers, removeSerializers, clearSerializers, getStasher

Am I missing something obvious?

Versions:

• Angular v18.1, TypeScript • json-stash v3.0.4

— Reply to this email directly, view it on GitHub https://github.com/dr-skot/json-stash/issues/1, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN74QB5TWDVPJPGOKIYI7LZMPROFAVCNFSM6AAAAABK4YFPP2VHI2DSMVQWIX3LMV43ASLTON2WKOZSGQYDQOJZGMYTAMI. You are receiving this because you are subscribed to this thread.Message ID: @.***>

dr-skot commented 1 month ago

Thanks for bringing this to my attention! Should be fixed in v3.1.0. Please let me know if you hit any other snags.

Oxydation commented 1 month ago

Thank you for such a speedy fix, it does work now! Yes, sure - will do.

Got one suggestion, writing a small decorator to register classes easier:

const classRegistry: (Type<unknown> | [Type<unknown>, string])[] = [];

export function registerClass(target: (Type<unknown> | [Type<unknown>, string])) {
  classRegistry.push(target);
}

export function getClassRegistry() {
  return classRegistry;
}

Which can be used like this:

@registerClass
export class MyClassToRegister {
  name: string;
}

And add all registered classes in an initializer class e.g. addClasses(...getClassRegistry());

dr-skot commented 1 month ago

Nice, I like that decorator. Might have time this week to try incorporating it, or something like it. Thanks!

On Mon, Jul 15, 2024, at 4:02 PM, Mathias wrote:

Thank you for such a speedy fix, it does work now! Yes, sure - will do.

Got one suggestion, writing a small decorator to register classes easier:

`const classRegistry: (Type | [Type, string])[] = [];

export function registerClass(target: (Type | [Type, string])) { classRegistry.push(target); }

export function getClassRegistry() { return classRegistry; } ` Which can be used like this:

@.*** export class MyClassToRegister { name: string; } `

And add all registered classes in an initializer class e.g. addClasses(...getClassRegistry());

— Reply to this email directly, view it on GitHub https://github.com/dr-skot/json-stash/issues/1#issuecomment-2229283496, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN74QFIP7QKTZ2LBPM6EYLZMQTGLAVCNFSM6AAAAABK4YFPP2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRZGI4DGNBZGY. You are receiving this because you commented.Message ID: @.***>

dr-skot commented 1 month ago

Hi, The latest version (v3.2.0) adds a @stashable decorator, used like this

@stashable() class X {...}

(Note the @.*** is actually a decorator factory.)

This is equivalent to

class X {...}

// ... then addClass(X)

@stashable can also be used the way you suggested: to register a bunch of classes and add them all later with addClasses. Just pass a "group" option.

@stashable({ group: "corporate" }) class Employee {...}

@stashable({ group: "corporate" }) class Department {...}

// ... then addClassses(...stashable.group("corporate"))

And you can supply custom save, load and update functions if necessary

@stashable({ save: (obj) => { / serialize obj to data, then / return data } load: (data) => { / build obj from data, then / return obj } update: (obj, data) => { / update obj with data / } }) class X {...}

There are more details in the README. Hope it's helpful! Scott

On Tue, Jul 16, 2024, at 9:00 AM, Scott Shepherd wrote:

Nice, I like that decorator. Might have time this week to try incorporating it, or something like it. Thanks!

On Mon, Jul 15, 2024, at 4:02 PM, Mathias wrote:

Thank you for such a speedy fix, it does work now! Yes, sure - will do.

Got one suggestion, writing a small decorator to register classes easier:

`const classRegistry: (Type | [Type, string])[] = [];

export function registerClass(target: (Type | [Type, string])) { classRegistry.push(target); }

export function getClassRegistry() { return classRegistry; } ` Which can be used like this:

@.*** export class MyClassToRegister { name: string; } `

And add all registered classes in an initializer class e.g. addClasses(...getClassRegistry());

— Reply to this email directly, view it on GitHub https://github.com/dr-skot/json-stash/issues/1#issuecomment-2229283496, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN74QFIP7QKTZ2LBPM6EYLZMQTGLAVCNFSM6AAAAABK4YFPP2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMRZGI4DGNBZGY. You are receiving this because you commented.Message ID: @.***>

Oxydation commented 4 weeks ago

Hello, I tried to use the @stashable decorator, but somehow it is not found (cannot find name 'stashable'). By looking through the npm package dist folder I could not find the necessary export. Am I missing something?

for the import itself import {stashable} from "json-stash" I get: TS2305: Module "json-stash" has no exported member stashable

dr-skot commented 4 weeks ago

Ack, I made that same export error again that you first wrote me about. Should be fixed now in v4.0.2. Thanks for your patience.

On Sat, Jul 27, 2024, at 5:31 AM, Mathias wrote:

Hello, I tried to use the @stashable https://github.com/stashable decorator, but somehow it is not found (cannot find name 'stashable'). By looking through the npm package dist folder I could not find the necessary export. Am I missing something?

for the import itself import {stashable} from "json-stash" I get: TS2305: Module "json-stash" has no exported member stashable

— Reply to this email directly, view it on GitHub https://github.com/dr-skot/json-stash/issues/1#issuecomment-2254092200, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAN74QHVGNE5RYDTLA56FDLZONSGPAVCNFSM6AAAAABK4YFPP2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENJUGA4TEMRQGA. You are receiving this because you commented.Message ID: @.***>

dr-skot commented 3 weeks ago

Fixed as of v4.0.2