@kosdev-code/kos-asset-manager 0.0.1-next.30 → 0.0.1-next.31

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.
package/README.md CHANGED
@@ -275,6 +275,38 @@ branching on an optional asset without the warning `wrapAsset` logs.
275
275
  A key resolves in the contexts named at the provider, first match winning,
276
276
  unless it names one: `other.app.id:brand/logo-primary`.
277
277
 
278
+ ## Loading data in a model
279
+
280
+ Json is declared like any other asset, which makes it fetchable by key:
281
+
282
+ ```ts
283
+ import { loadAsset } from '@kosdev-code/kos-asset-manager';
284
+
285
+ @kosModel({ modelTypeId: MODEL_TYPE })
286
+ export class MenuModelImpl {
287
+ async load(): Promise<void> {
288
+ const menu = await loadAsset<MenuDto>('config/menu');
289
+ if (!menu) return;
290
+ this.reconcile(menu.items);
291
+ }
292
+ }
293
+ ```
294
+
295
+ Data usually has to be parsed and normalised into a model's own shape
296
+ anyway, so this is deliberately a plain async call rather than something a
297
+ component reads. The point is the key: the file can be renamed, moved, or
298
+ moved into a different kab without the model changing.
299
+
300
+ Undefined comes back when the key isn't declared, the fetch fails, or the
301
+ content isn't valid json, each logged with the key — so a model handles it
302
+ the way it already handles a service returning nothing. `loadAssetText(key)`
303
+ is the same without the parse.
304
+
305
+ Json files are declared and keyed like anything else, but they don't count
306
+ as media when `kos-assets init` works out where a project keeps its media —
307
+ a manifest sitting beside a folder of images is what marks the images as the
308
+ media, not the whole folder.
309
+
278
310
  ## Selecting sets
279
311
 
280
312
  Sets of assets are selected rather than named, so adding a file to a folder
@@ -45,6 +45,21 @@ const TYPES = {
45
45
  '.webm': 'video/webm',
46
46
  '.webp': 'image/webp',
47
47
  '.woff2': 'font/woff2',
48
+ '.json': 'application/json',
49
+ };
50
+
51
+ /**
52
+ * Data assets are declared like any other, but they are not what makes a
53
+ * directory a project's media directory -- a brandset keeps its manifest
54
+ * beside its images, and it is the manifest sitting there that tells you the
55
+ * images are the media rather than the whole folder. So layout detection
56
+ * looks past them.
57
+ */
58
+ const DATA_TYPES = new Set(['.json']);
59
+
60
+ const isMedia = (name) => {
61
+ const ext = name.slice(name.lastIndexOf('.')).toLowerCase();
62
+ return Boolean(TYPES[ext]) && !DATA_TYPES.has(ext);
48
63
  };
49
64
 
50
65
  /**
@@ -280,8 +295,7 @@ const scanMedia = (dir, prefix = '') =>
280
295
  if (item.name.startsWith('.') || SKIP_DIRS.has(item.name)) return [];
281
296
  const rel = prefix ? `${prefix}/${item.name}` : item.name;
282
297
  if (item.isDirectory()) return scanMedia(join(dir, item.name), rel);
283
- const ext = item.name.slice(item.name.lastIndexOf('.')).toLowerCase();
284
- return TYPES[ext] ? [rel] : [];
298
+ return isMedia(item.name) ? [rel] : [];
285
299
  });
286
300
 
287
301
  /** The deepest directory every one of these paths sits under. */
@@ -319,7 +333,7 @@ const climbToMedia = (projectRoot, dir) => {
319
333
  const onlyMedia = entries.every((item) =>
320
334
  item.isDirectory()
321
335
  ? holdsMedia(resolve(projectRoot, ...parent, item.name))
322
- : TYPES[item.name.slice(item.name.lastIndexOf('.')).toLowerCase()]
336
+ : isMedia(item.name)
323
337
  );
324
338
  if (!onlyMedia) {
325
339
  break;
@@ -524,6 +538,8 @@ export {
524
538
  SOURCE_DIR,
525
539
  ASSETS_ROOT,
526
540
  TYPES,
541
+ DATA_TYPES,
542
+ isMedia,
527
543
  mediaSource,
528
544
  mediaRoot,
529
545
  scanMedia,
package/index.d.ts CHANGED
@@ -5,6 +5,7 @@ export { registerAssetModels } from './registration';
5
5
  export { contextNames } from './runtime/assets';
6
6
  export { KosAssetProvider, KosAssetContext, useAssetManager, useKosAssetContext, } from './runtime/asset-provider';
7
7
  export { resolveAsset, wrapAsset, hasAsset, listAssets } from './runtime/resolve';
8
+ export { loadAsset, loadAssetText } from './runtime/data';
8
9
  export { preloadAssets, preloadDeclared, releaseAssets, } from './runtime/preload';
9
10
  export type { AssetSelector, PreloadOptions, PreloadProgress, ResolvedAsset, } from './runtime/types';
10
11
  //# sourceMappingURL=index.d.ts.map