feat: add data access and feature libraries for return process, including models, schemas, and routes

This commit is contained in:
Lorenz Hilpert
2025-03-20 21:25:20 +01:00
parent fbd5414e47
commit db7da0699e
91 changed files with 1477 additions and 67 deletions

View File

@@ -1,5 +1,6 @@
export * from './lib/idb.storage-provider';
export * from './lib/local.storage-provider';
export * from './lib/session.storage-provider';
export * from './lib/signal-store-feature';
export * from './lib/storage-provider';
export * from './lib/storage';

View File

@@ -0,0 +1,26 @@
import { Type } from '@angular/core';
import { getState, patchState, signalStoreFeature, withHooks, withMethods } from '@ngrx/signals';
import { StorageProvider } from './storage-provider';
import { injectStorage } from './storage';
export function withStorage(storageKey: string, storageProvider: Type<StorageProvider>) {
return signalStoreFeature(
withMethods((store, storage = injectStorage(storageProvider)) => ({
storeState: () => {
const state = getState(store);
storage.set(storageKey, state);
},
restoreState: async () => {
const data = await storage.get(storageKey);
if (data) {
patchState(store, data);
}
},
})),
withHooks((store) => ({
onInit() {
store.restoreState();
},
})),
);
}

View File

@@ -18,7 +18,7 @@ export class Storage {
return this.storageProvider.set(key, value);
}
async get<T>(token: string | object, schema?: z.ZodType<T>): Promise<T | undefined> {
async get<T>(token: string | object, schema?: z.ZodType<T>): Promise<T | unknown> {
let key: string;
if (typeof token === 'string') {
@@ -31,13 +31,13 @@ export class Storage {
if (schema) {
return await schema.parse(data);
}
return undefined;
return data;
}
}
const storageMap = new WeakMap<Type<StorageProvider>, Storage>();
export function storage(storageProvider: Type<StorageProvider>): Storage {
export function injectStorage(storageProvider: Type<StorageProvider>): Storage {
if (storageMap.has(storageProvider)) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return storageMap.get(storageProvider)!;