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

@edvinas1122/api_wrapper

v1.2.0

Published

class offering generating API endpoint fetch methods

Downloads

44

Readme

wrapAPI

npm page: https://www.npmjs.com/package/@edvinas1122/api_wrapper

api_wrapper is a simple wrapper that lets explicitly define API endpoints in a configuration file.

It is an API layer abstraction.

If you interested in api wrapping, generating automated tests, documentation, check swagger api here. This project meant to be learning.

Installation

To add module run in node project root dir

npm i @edvinas1122/api_wrapper

Initialising

To initialize wrapper follow this configuration pattern. Recommended api.conf.ts

export enum NotionEndpoints {
    getPage = 'getPage',
    getBlockChildren = 'getBlockChildren',
    getBlock = 'getBlock',
    getDatabase = 'getDatabase',
    queryDatabase = 'queryDatabase',
    getPagePropertyItem = 'getPagePropertyItem',
    search = 'search',
}

const notionAPIConfig: APIInfo = {
    name: 'NotionAPI',
    apiBaseUrl: 'https://api.notion.com/v1/',
    headers: {
        'Notion-Version': '2022-06-28',
        'Authorization': `Bearer ${process.env.ACCESS_API_KEY}`, // Assuming authToken is available in the scope.
    },
    endpoints: [
        { name: NotionEndpoints.getPage, path: 'pages/:pageId', method: 'GET' },
        { name: NotionEndpoints.getBlockChildren, path: 'blocks/:blockId/children', method: 'GET' },
        { name: NotionEndpoints.getBlock, path: 'blocks/:blockId', method: 'GET' },
        { name: NotionEndpoints.getDatabase, path: 'databases/:databaseId', method: 'GET' },
        { name: NotionEndpoints.queryDatabase, path: 'databases/:databaseId/query', method: 'POST' },
        { name: NotionEndpoints.getPagePropertyItem, path: 'pages/:pageId/properties/:propertyId', method: 'GET' },
        { name: NotionEndpoints.search, path: 'search', method: 'POST' },
    ],
};

then allocate your api object like this

const notionAPI = new API(notionAPIConfig); // or API<NotionEndpoints>(notionAPIConfig)

or recommended

export default class NotionAPI extends API<NotionEndpoints> {
    constructor() {
        super(notionAPIConfig);
    }
}

Use

To use the new api wrapper

notionAPI.getPage({
			params: { pageId: "here goes notion page" },
		});

An old way of alpha 2 interaction

notionAPI.interact({endpoint_name: NotionEndpoints.getPage, params:{pageId: "notion_page_id_here"}});

Adjust interaction with a custom service wrapper

import NotionAPI from "./api";

export default class NotionService {
	constructor(
		private api: NotionAPI,
	) {}

	async getPage(pageId?: string) {
		return this.api.getPage({
			params: pageId ? { pageId: pageId } : undefined,
		});
	}

	async getDatabase(databaseId?: string) {
		return this.api.getDatabase({
			params: databaseId ? { databaseId: databaseId } : undefined,
		});
	}

Features

Default Parameters

If params or body are not provided in method parameter then the default configuration would be addressed.

Initialize default parameters like this

const rootPageDir = process.env.NEXT_PUBLIC_NOTION_ROOT_PAGE;

notionAPIConfig.defaultParams = {
    getPage: {
        params: { pageId: rootPageDir },
    },
    getBlockChildren: {
        params: { blockId: rootPageDir },
        body: { count: 200 },
    },
    getBlock: {
        params: { blockId: rootPageDir },
    },
    search: {
        body: {
            query: '',
            sort: {
                direction: 'ascending',
                timestamp: 'last_edited_time',
            },
            filter: {
                value: 'page',
                property: 'object',
            },
        },
    },
};

The default parameters are overridden by provided method parameters, but only for a single scope, so:

notionAPI.interact({
	endpoint_name: NotionEndpoints.search,
	body:{
		sort: {
			direction: 'decending'
		}
	}
});

Would result into final the headers like this:

sort: {
	direction: 'decending'
},

Multiple APIs per one wrapper (Deprecated since alpha 2)

Possibility to set an array of API definitions and interact through a same wrapper.

const Configs = [notionConfig, openAIConfig];

const APIset = new API(Configs);

APIset.interact({apiName: 'NotionAPI', NotionEndpoints.getPage});