The @ginjou/with-rest-api package provides a flexible fetcher for connecting Ginjou to standard RESTful APIs. It is specifically designed to follow the conventions established by json-server.
Install the RESTful API provider and its peer dependency:
pnpm add @ginjou/with-rest-api
yarn add @ginjou/with-rest-api
npm install @ginjou/with-rest-api
bun add @ginjou/with-rest-api
To use the RESTful API provider, initialize the fetcher and register it in your root component.
In a Vue application, use defineFetchersContext within your App.vue setup.
<script setup lang="ts">
import { defineFetchersContext } from '@ginjou/vue'
import { createFetcher } from '@ginjou/with-rest-api'
defineFetchersContext({
default: createFetcher({
url: 'https://api.example.com'
})
})
</script>
<template>
<RouterView />
</template>
For Nuxt applications, register the provider in your root app.vue component.
<script setup lang="ts">
import { defineFetchersContext } from '@ginjou/vue'
import { createFetcher } from '@ginjou/with-rest-api'
defineFetchersContext({
default: createFetcher({
url: 'https://api.example.com'
})
})
</script>
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
The createFetcher implementation relies on specific query parameters and header conventions common in the json-server ecosystem.
The fetcher translates Ginjou's pagination state into _start and _end query parameters.
_start: The zero-based index of the first item to return._end: The zero-based index of the last item (exclusive).x-total-count HTTP header. If this header is missing, it will default to the length of the returned data array.Sorting is handled via _sort and _order parameters.
_sort: A comma-separated list of fields to sort by._order: A comma-separated list of sort directions (asc or desc).Filters are mapped to query parameters using field suffixes:
| Operator | Parameter Suffix | Example |
|---|---|---|
eq | (none) | title=hello |
ne | _ne | id_ne=1 |
gte | _gte | views_gte=10 |
lte | _lte | views_lte=20 |
contains | _like | title_like=ginjou |
q is provided, it is sent as a global search parameter (?q=keyword) as per json-server specifications.json-server specification, certain advanced filtering features are not supported:or and and logical operators are not supported. Using these in your filters will result in an error.