README

Type-safe TypeScript client for the Portal DB API, generated from OpenAPI specification using openapi-typescript and openapi-fetch.

Installation

Install (npm)
npm install @grovepath/portal-db-ts-sdk openapi-fetch

Usage

Basic GET Request

basic-get.ts
import createClient from 'openapi-fetch';
import type { paths } from '@grovepath/portal-db-ts-sdk';

const client = createClient<paths>({
  baseUrl: 'http://localhost:3000',
  headers: {
    'Authorization': `Bearer ${JWT_TOKEN}`,
  },
});

// Fetch all services
const { data, error } = await client.GET('/services');

if (error) {
  console.error('Error:', error);
} else {
  console.log('Services:', data);
}

Query with Filters

get-with-filters.ts
// GET with PostgREST filters
const { data, error } = await client.GET('/services', {
  params: {
    query: {
      active: 'eq.true',              // Filter: active = true
      service_id: 'like.*ethereum*',  // Pattern match
    }
  }
});

React with @tanstack/react-query

For React applications, you can use openapi-react-query for a type-safe wrapper around React Query:

Install (React)
npm install openapi-react-query openapi-fetch @tanstack/react-query
react-query-usage.tsx
import createFetchClient from 'openapi-fetch';
import createClient from 'openapi-react-query';
import type { paths } from '@grovepath/portal-db-ts-sdk';

const fetchClient = createFetchClient<paths>({
  baseUrl: 'http://localhost:3000',
  headers: {
    'Authorization': `Bearer ${JWT_TOKEN}`,
  },
});

const $api = createClient(fetchClient);

const ServicesComponent = () => {
  const { data, error, isLoading } = $api.useQuery(
    'get',
    '/services',
    {
      params: {
        query: {
          active: 'eq.true',
        }
      }
    }
  );

  if (isLoading || !data) return 'Loading...';
  if (error) return `Error: ${error.message}`;

  return (
    <div>
      {data.map(service => (
        <div key={service.id}>{service.service_id}</div>
      ))}
    </div>
  );
};

Documentation

  • openapi-fetch: https://openapi-ts.dev/openapi-fetch/

  • openapi-react-query: https://openapi-ts.dev/openapi-react-query/

  • PostgREST API Reference: https://postgrest.org/en/stable/references/api/tables_views.html

Was this helpful?