npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@mmuscat/angular-resource

v0.1200.0-next.1

Published

Data fetching library for Angular Composition API

Downloads

1

Readme

Angular Resource

Data fetching library for Angular Composition API.

Quick Start

Install via NPM


npm install @mmuscat/angular-resource

Install via Yarn


yarn add @mmuscat/angular-resource

Example

Query

function getTodosByUserId() {
   const http = inject(HttpClient)
   return function (userId) {
      return http.get(url, {
         params: { userId },
      })
   }
}

const GetTodosByUserId = new Query(getTodosByUserId)

Mutation

function createTodo() {
   const http = inject(HttpClient)
   return function (data) {
      return http.post(url, data)
   }
}

const CreateTodo = new Mutation(createTodo, {
   operator: exhaustAll,
})

Usage

function setup() {
   const userId = use("123")
   const [createTodoStatus, createTodo] = inject(CreateTodo)
   const getTodosByUserId = inject(GetTodosByUserId)
   const todos = getTodosByUserId(userId, {
      refetch: [createTodo],
      initialValue: [],
   })

   return {
      todos,
      createTodo,
      createTodoStatus,
   }
}

@Component({
   template: `
      <spinner *ngIf="todos.pending"></spinner>
      <div *ngIf="todos.error">Something went wrong</div>
      <todo *ngFor="let todo of todos.value"></todo>
      <add-todo (save)="createTodo($event)"></create-todo>
   `,
})
export class MyComponent extends ViewDef(setup) {}

Api Reference

Query

Queries are services created from factory functions that return another function that produces an observable stream. Supports dependency injection.

function myQuery() {
   const http = inject(HttpClient)
   return function (params) {
      return http.get(url, {
         params,
      })
   }
}

const MyQuery = new Query(myQuery)

Inject the token in a component and create the query params and an initial value.

function setup() {
   const myQuery = inject(MyQuery)
   const params = use(Function)
   const result = myQuery(params, {
      initialValue: null,
   })
   return {
      result,
   }
}

This returns a Value that emits Resource notifications.

QueryConfig

interface QueryConfig {
   operator?: <T>() => OperatorFunction<Observable<T>, T> // defaults to switchMap
}

The default operator used to map higher order observables can be overridden.

const MyQuery = new Query(myQuery, {
   operator: concatMap,
})

QueryOptions

interface QueryOptions<T> {
   initialValue: T
   refetch?: Observable<any>[]
}

The query can be configured with refetch to pull fresh data whenever another stream emits a value. If refetch is given a Resource observable, it will wait until it is done before it runs the query again.

function setup() {
   const myQuery = inject(MyQuery)
   const [mutation, mutate] = inject(MyMutation)
   const fetch = use(Function)
   const result = myQuery(fetch, {
      initialValue: null,
      refetch: [mutation],
   })
}

Caching

Queries are memoized by stringifying arguments. Make sure the params passed to queries are serializable to JSON.

function setup() {
   const myQuery = inject(MyQuery)
   const fetch = use(Function)
   const result = myQuery(fetch, {
      initialValue: null
   })
   fetch([1, 2, 3]) // stringified to "[1, 2, 3]" to use as cache key
}

Mutation

Mutations are services created from factory functions that return another function that produces an observable stream.

function myMutation() {
   const http = inject(HttpClient)
   return function (params) {
      return http.post(url, params)
   }
}

const MyMutation = new Mutation(myMutation)

MutationConfig

interface MutationConfig {
   operator?: <T>() => OperatorFunction<Observable<T>, T> // defaults to exhaust
}

The default operator used to flatten higher order observables can be overridden.

const MyMutation = new Mutation(myMutation, {
   operator: concat,
})

Returning mutation from a ViewDef

Use the array destructure pattern to obtain an Emitter that can be used to trigger the mutation outside the ViewDef factory.

function setup() {
   const [mutation, mutate] = inject(MyMutation)

   return {
      mutation, // mutation status
      mutate, // trigger mutation
   }
}

@Component({
   template: `
      <spinner *ngIf="mutation.pending"></spinner>
      <button (click)="mutate(params)">Mutate</button>
   `,
})
export class MyComponent extends ViewDef(setup) {}

Resource

Interface that represents the state of a Query or Mutation

interface Resource<T> {
   value: T
   error: unknown
   pending: boolean
   done: boolean
}

pending is true if there are any active queries or mutations in the queue. done is true when there are no more pending transactions, until the next request is made. error is set when an error is caught and resets when a new request is made.

cancel

Cancel pending queries

const getTodosByUserId = inject(GetTodosByUserId)
const todos = getTodosByUserId(userId)

cancel(todos)

Cancel pending mutations

const createTodo = inject(CreateTodo)

createTodo(todo)

cancel(createTodo)

invalidate

Invalidate a single query

const getTodosByUserId = inject(GetTodosByUserId)
const todos = getTodosByUserId(userId)

invalidate(todos)

Invalidate a single query with specific arguments

const getTodosByUserId = inject(GetTodosByUserId)
const todos = getTodosByUserId(userId)

invalidate(todos, "123")

Invalidate all queries by injection token

const getTodosByUserId = inject(GetTodosByUserId)
const todos = getTodosByUserId(userId)

invalidate(GetTodosByUserId)