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

next-model-api-client-connector

v0.2.0

Published

Client side connector for NextModel Api Endpoints.

Downloads

3

Readme

NextModelApiClientConnector

Api client for ApiServer using NextModel package.

Build Status

Features:

  • Shared model for Server and Client.
  • Supports all server side supported queries
  • Allows Api-Endpoint to de on a different domain
  • Api Versioning
  • Custom Routes and Actions

Roadmap / Where can i contribute

See GitHub project for current progress/tasks

  • Fix Typos
  • Add user credentials to queries
  • Add more examples
  • Add exists, join and subqueries
  • There are already some tests, but not every test case is covered.

TOC

Example

The connector fetches all settings from the class or base class used.

const User = class User extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get routeDomain() {
    return 'http://api.example.com'
  }

  static get modelName() {
    return 'User';
  }

  static get schema() {
    return {
      id: { type: 'integer' },
      name: { type: 'string' },
    };
  }
}

Create an base model with the connector to use it with multiple models.

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }
});

const User = class User extends BaseModel {
  static get modelName() {
    return 'User';
  }

  static get schema() {
    return {
      id: { type: 'integer' },
      name: { type: 'string' },
    };
  }
}

const Address = class Address extends BaseModel {
  static get modelName() {
    return 'Address';
  }

  static get schema() {
    return {
      id: { type: 'integer' },
      street: { type: 'string' },
    };
  }
}

Custom Routes

The route configuration is done at the defined (shared) Model.

Default Routes:

all    : POST /users
first  : POST /users/first
last   : POST /users/last
count  : POST /users/count
insert : POST /users/create
update : POST /user/:id
delete : POST /user/:id/delete

Domain

The domain needs to be defined when your Api client is running on a different domain than the Backend.

Default: ''

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get routeDomain() {
    return 'http://api.example.com';
  }
}

const User = class User extends BaseModel { ... }
all    : POST http://api.example.com/users
first  : POST http://api.example.com/users/first
last   : POST http://api.example.com/users/last
count  : POST http://api.example.com/users/count
insert : POST http://api.example.com/users/create
update : POST http://api.example.com/user/:id
delete : POST http://api.example.com/user/:id/delete

IsRelative

Set routeIsRelative to true to prevent adding the leading / to the Url. Setting does not work when domain is present.

Default: false

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get routeIsRelative() {
    return true;
  }
}

const User = class User extends BaseModel { ... }
all    : POST users
first  : POST users/first
last   : POST users/last
count  : POST users/count
insert : POST users/create
update : POST user/:id
delete : POST user/:id/delete

Path

The routePath defines the base route on the Server which contains the Api.

Default: ''

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get routePath() {
    return 'api';
  }
}

const User = class User extends BaseModel { ... }
all    : POST /api/users
first  : POST /api/users/first
last   : POST /api/users/last
count  : POST /api/users/count
insert : POST /api/users/create
update : POST /api/user/:id
delete : POST /api/user/:id/delete

Version

The Api can be versioned with routeVersion.

Default: ''

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get routePath() {
    return 'api';
  }

  static get routeVersion() {
    return 'v1';
  }
}

const User = class User extends BaseModel { ... }
all    : POST /api/v1/users
first  : POST /api/v1/users/first
last   : POST /api/v1/users/last
count  : POST /api/v1/users/count
insert : POST /api/v1/users/create
update : POST /api/v1/user/:id
delete : POST /api/v1/user/:id/delete

Name

The model name in the route defaults to the tableName, but can be overwritten by routeName.

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get routeName() {
    return 'account';
  }
}

const User = class User extends BaseModel { ... }
all    : POST /accounts
first  : POST /accounts/first
last   : POST /accounts/last
count  : POST /accounts/count
insert : POST /accounts/create
update : POST /account/:id
delete : POST /account/:id/delete

Postfix

The routePostfix can be used to define a file name for the Url.

Default: ''

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get routePostfix() {
    return '.json';
  }
}

const User = class User extends BaseModel { ... }
all    : POST /users.json
first  : POST /users/first.json
last   : POST /users/last.json
count  : POST /users/count.json
insert : POST /users/create.json
update : POST /user/:id.json
delete : POST /user/:id/delete.json

Action Path

The action url can be defined for every action.

Defaults:

  • all: ''
  • first: '/first'
  • last: '/last'
  • count: '/count'
  • create: '/create'
  • update: ''
  • delete: '/_delete'
const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get allActionPath() {
    return '/all';
  }

  static get firstActionPath() {
    return '';
  }

  static get updateActionPath() {
    return '/update';
  }
}

const User = class User extends BaseModel { ... }
all    : POST /users/all
first  : POST /users
last   : POST /users/last
count  : POST /users/count
insert : POST /users/create
update : POST /user/:id/update
delete : POST /user/:id/delete

Action Method

The method url can be defined for every action.

Default: 'POST'

Please Note: The default is POST for every route, cause the filter payload can be too large for query strings.

const BaseModel = class BaseModel extends NextModel {
  static get connector() {
    return NextModelApiClientConnector;
  }

  static get allActionMethod() {
    return 'GET';
  }

  static get firstActionMethod() {
    return 'GET';
  }

  static get lastActionMethod() {
    return 'GET';
  }

  static get countActionMethod() {
    return 'GET';
  }

  static get updateActionMethod() {
    return 'PATCH';
  }

  static get deleteActionMethod() {
    return 'DELETE';
  }
}

const User = class User extends BaseModel { ... }
all    : GET    /users
first  : GET    /users/first
last   : GET    /users/last
count  : GET    /users/count
insert : POST   /users/create
update : PATCH  /user/:id
delete : DELETE /user/:id/delete

Changelog

See history for more details.

  • 0.1.0 2017-03-15 First release compatible with NextModel 0.4.0
  • 0.1.1 2017-04-05 Updated next-model dependency
  • 0.2.0 2017-04-05 First public release