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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@voire/vuerest

v1.2.0

Published

Set of composables for RESTful API calls in Clean architecture.

Downloads

3

Readme

vuerest

Set of composables for RESTful API calls in Clean architecture.

vuerest

Usage

Methods

Generate and use type safe methods to call RESTful API.

const readUsers = useRestRead<TDataFromAPI, TUserModel, TFilters>('/api/users', mapDataToUser)

...
const mappedUsers = await read({ status: 'active' })

All methods: | name | corresponding RESTful method | | --- | --- | | useRestRead | GET | | useRestFind | GET by id | | useRestCreate | POST | | useRestReplace | PUT | | useRestUpdate | PATCH | | useRestRemove | DELETE by id | | useRestBatchRemove | DELETE |

Domains

This makes more sense if we use not just separated methods but whole domains - sets of different methods for one API app.

Let's say, we have an API app with read (GET), find (GET by id), and create (POST) methods. In this case, we may just generate multiple methods with one combined composable:

const { find, read, create, update, replace, remove } = useRestReadableDomain<...>( ... )

Or even for all the RESTful methods:

const { find, read, create, } = useRestDomain<...>( ... )

You can use these combinations just as composables or move such logic onto separated level using it as a part of some domain-specific store.

All domain methods: | name | returned methods | | --- | --- | | useRestReadableDomain | read, find | | useRestWritableDomain | create, replace, update | | useRestReadWritableDomain | read, find, create, replace, update | | useRestDomain | read, find, create, replace, update, remove |

Custom methods' combinations

Feel free to 'construct' your own composables for specific domains with separated methods:

export function useThatOneDomain<...>(
  url: MaybeRef<string>,
  getMapper: Mapper<TGetData, TGetModel>,
  readMapper: Mapper<TReadData, TReadModel>,
  domainOptions?: FetchOptions<'json'>,
) {
  return {
    ...useRestReadableDomain<...>(url, getMapper, readMapper, domainOptions),
    create: useRestCreate<...>(url, getMapper, domainOptions),
    remove: useRestRemove<...>(url, domainOptions),
  }
}

Additional http client's options

Under the hood, we use ohmyfetch, so you may provide additional options for each method OR for a whole domain (check domainOptions argument), like interceptors, abort signal etc.

Go deeper into ohmyfetch repo to get details about the usage.

Yeah, but ...why?

It's all about Clean architecture and separating business logic / app logic and UI.

Let's say, we describe our business entities with Data (representation fetched from API) and Models (representation we use on client side).

type RecordData = {
  id: number
  created_at: string
}

type RecordModel = {
  id: number
  createdAt: Date
}

So, we need to refer to the API, get raw data and map it into client-acceptable model that will be comfortable to use within all the front-end application.

function mapRecord(data: RecordData): RecordModel {
  return {
    id: data.id,
    createdAt: new Date(data.created_at),
  }
}

Also, probably we will want to create typed domain just only one time (in some state) instead of specifing typings on every call inside a component.

If your approach to writing enterprise applications is quite similar, maybe these composables meet your needs.

Nuxt module

Check this repo to auto import the composables in your Nuxt application.

Note

Only Nuxt 3 is supported