@void-snippets/angular
createResourceInject
Factory that creates four Signal-based inject functions for a resource.
#What it does
The main factory. Give it a cache key prefix and your AngularResourceService subclass (injected from DI), and it returns four injection functions — injectList, injectGet, injectMutations, and injectInfinite. All types flow from the service's generic parameters — you write no generics at the call site.
#Signature
typescript
function createResourceInject<S extends IResourceService>(
resourceKey: string,
service: S,
options?: {
adapters?: VSAdapters;
defaultParams?: VSQueryParams;
optimistic?: VSAngularOptimisticHandlers;
},
): { injectList; injectGet; injectMutations; injectInfinite }#Parameters
| Argument | Type | Description |
|---|---|---|
resourceKey | string | TanStack Query cache namespace. Typically the resource name: 'contacts', 'orders'. |
service | AngularResourceService instance | Your service instance — types for all four inject functions are inferred from this. |
options | optional | Adapters, default page params, optimistic update handlers. |
Injection context requirement. Every
inject*call must run inside Angular's injection context — a constructor, a field initializer,runInInjectionContext, or a function called synchronously during component construction.
#Example — factory file (created once per resource)
typescript
// contacts/contacts.resource.ts
import { inject } from '@angular/core';
import { createResourceInject } from '@void-snippets/angular';
import { ContactsService } from './contacts.service';
import type { Contact } from './contacts.types';
export function useContactResource() {
const service = inject(ContactsService); // ← runs in injection context
return createResourceInject('contacts', service, {
defaultParams: { page: 1, limit: 20 },
optimistic: {
update: (cache, { _id, payload }) =>
cache.map((c) => (c._id === _id ? { ...c, ...payload } : c)),
remove: (cache, id) =>
cache.filter((c) => c._id !== id),
create: (cache, { payload, tempId }) => [
{ ...payload, _id: tempId as Contact.Id },
...cache,
],
onError: (err, op) => console.error(`Failed to ${op.kind}:`, err.message),
},
});
}