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

apollo-datasource-configurable-rest

v1.2.1

Published

configurable rest datasource

Downloads

9

Readme

Apollo Configurable REST Data Source

This rest datasource is an extension of apollo's rest data source. So you have all the options which come from apollo's rest data source and even more:

This extension aims to simplify data source definitions in your apollo server by configuring them via class parameters.

Installation

yarn add apollo-datasource-configurable-rest
# or
npm i apollo-datasource-configurable-rest

Usage

One key thought, besides the configuration via class parameter, is that all keywords which are prefixed with a $ are replaced by the query argument with the same name.

For example if you have a query argument named language and you use $language in your definitions it will be replaced by the query argument.

Simple example

The following example shows how to configure a get request with url params and headers:

import { ConfigurableRESTDataSource } from 'apollo-datasource-configurable-rest'

export class MyRestDatasource extends ConfigurableRESTDataSource {

  // $book will be replaced by the query argument named "book"
  baseURL = 'https://api.com/texts/$book'

  // if your query contains optional arguments 
  // you can set a default value for these arguments here
  defaultArgs = {
    book: 'title-of-book'
    language: 'de',
    chapter: 1,
  }

  // url parameter definitions
  // with the default arguments this would result in:
  // https://api.com/texts/title-of-book?lng=de&chapter=1
  params = {
    lng: "$language",
    chapter: "$chapter",
  }

  // header definitions
  // the $accessToken keyword would replaced by
  // the query argument named "accessToken"
  headers = {
    Authorization: 'Bearer $accessToken',
    'Content-Type': 'application/json',
  }

  // this is for your resolver
  async fetchChapters(args: QueryArgs) {
    // the configuredGET command now replaces the arguments
    // in the url, parameters and headers and fetches the api
    return this.configuredGET(args)
  }
}

Configured fetchers

There are fetchers GET, DELETE, POST, PUT and PATCH.

async configuredGET<TResult = any>(args: Partial<TArgs>, options?: RequestInitOptions): Promise<TResult>

async configuredDELETE<TResult = any>(args: Partial<TArgs>, options?: RequestInitOptions): Promise<TResult>

async configuredPOST<TResult = any>(args: Partial<TArgs>, body?: Body): Promise<TResult>

async configuredPUT<TResult = any>(args: Partial<TArgs>, body?: Body): Promise<TResult>

async configuredPATCH<TResult = any>(args: Partial<TArgs>, body?: Body): Promise<TResult>

Caching

You can configure a cache time for the GET and DELETE fetcher:

public fetchChapters(args: QueryArgs) {
  this.configuredGET(args, { cacheTime: 3000 })
}

Use Generics

You can define typings for your query arguments, url parameters, headers and body definitions:

import { ConfigurableRESTDataSource } from 'apollo-datasource-configurable-rest'

type QueryArgs = {
  book?: string,
  language?: string,
  chapter?: number,
  accessToken: string,
}

type Params = {
  lng: string,
  chapter: number,
}

type Headers = {
  Authorization: string 
}

type Body = {
  myBody: {
    structure: string,
    num: number,
  }
}

export class MyRestDatasource extends ConfigurableRESTDataSource<
  QueryArgs, Params, Headers, Body
> {
  // ...
}