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

atshop-service-models

v4.1.7

Published

TypeScript service models for ATShop

Downloads

64

Readme

TypeScript Service Models for ATShop 2.0

A collection of models for interacting with the ATShop API. Easily create, read, update and delete ATShop resources.

npm node

NOTICE This is still very much a work in progress. The documentation for this package is mostly provided exclusively through TypeScript type hints.

Installation & Setup

There's a couple steps to getting started with atshop-service-models.

1. Install

Just pull in the package through npm.

npm install atshop-service-models

2. Install dependencies.

Depending on where you're implementing these models, you'll also need to pull in and setup a Feathers client.

npm install @feathersjs/feathers @feathersjs/socketio-client socket.io-client

3. Setting up the Feathers client.

To set up atshop-service-models, you'll first need to get your API client up and running.

import Feathers from '@feathersjs/feathers';
import FeathersIO from '@feathersjs/socketio-client';
import SockeIO from 'socket.io-client';

const SocketClient = FeathersIO(SockeIO('https://api.atshop.io'));
const FeathersClient = Feathers();

FeathersClient.configure(SocketClient);

4. Setting up atshop-service-models

import { ATShopServiceModels } from 'atshop-service-models';

FeathersClient.configure(ATShopServiceModels());

Setup complete!

Since we're using FeatherJS as our API intermediary, atshop-service-models can run in either a browser or on your Node.js server. This can be very handy if you're deploying your own storefront and don't want to depend on any backend other than ATShop.io.

Shops

Fetch a shop by domain or ID.

import { ShopModel } from 'atshop-service-models';

// Fetch a shop by subdomain: (e.g. test-shop.atshop.io)
let Shop = await ShopModel.find({ domain: 'test-shop' }).fetchOne();

// Fetch a shop by custom domain: (e.g. example.com)
Shop = await ShopModel.find({ customDomain: 'example.com' });

// Fetch a shop by ID
Shop = await ShopModel.get('ZBAWZE4LzB4RoguGY');

Fetch a shop's products.

const products = await Shop.products.fetch(); // Returns an instance of PaginatedServiceModel.

products.data.forEach((product) => {
    console.log(product.name);          // Product name.
    console.log(product.description);   // Product description markdown.
    console.log(product.minQuantity);   // Minimum order quantity for the product.
    console.log(product.stockCount);    // Remaining stock.
    console.log(product.value);         // Dinero.js instance of the product price.
                                        // https://sarahdayan.github.io/dinero.js/
});

Fetch a shop's categories

const categories = await Shop.categories.fetch();

categories.data.forEach((category) => {
    console.log(category.title);    // Category heading
    console.log(category.slug);     // Category slug
    console.log(category.position); // Position of category relative to other categories.
});

Orders

Get an order by ID

import { OrderModel } from 'atshop-service-models';

const order = await OrderModel.get('orderIdHere');

console.log(await order.product);       // Product ordered. 
console.log(await order.humanValue());  // Order value in a human format.
console.log(order.quantity);            // Quantity of product ordered.
console.log(order.description);         // Order description.

Models

All atshop-service-models models come equipped with a set of reusable methods and properties that are used to interact with the API.

  • static App Global Feathers.js application object atshop-service-models
  • static service Feathers.js service object for the current model.
  • static create(data) Create a new resource for the current model. Returns an instance of self.
  • static find(query) Build a query for the current model. Returns an instance of PaginatedServiceModel.
  • static get(_id) Fetch a single entry from the service by ID.
  • patch(data) Merge the given data with the current model instance and send the changes to the API.
  • delete() Delete the current model entry.

Adding your own properties and methods

You can rather easily extend a model with your own functionality if you have any implementation specific needs.

import { OrderModel as OrderServiceModel } from 'atshop-service-models';

class OrderModel extends OrderServiceModel {
    
    get emailer() {
        return new MyEmailerService({ defaultEmail: this.email });
    }
    
    sendThankYouEmail() {
        this.emailer.send({ message: 'Thank you for placing an order!' });
    }
    
}

// Send email.
OrderModel.get('someOrderId').then((order) => order.sendThankYouEmail());

To ensure your model is utilized for for future relationship calls. (e.g. ShopModel.products), you'll need to register it during the configuration process.

FeathersClient.configure(ATShopServiceModels({
    models: {
        OrderModel, // Referencing the OrderModel you see up above.
    }
}));

Now, any relationship that returns an OrderModel (e.g. OrderFeedbackModel.order) will be an instance of your custom OrderModel.

TypeScript users:

If you're a TypeScript user, you'll likely need to override the return types of model to model relationships.

class MyCustomProductModel extends ProductModel {
    
    // "Belongs to" and "has one" relationships return a promise.
    shop!: Promise<MyCustomShopModel> // Now you'll get proper type-hinting for any calls to the shop relationship on MyCustomProductModel.
    
}

class MyCustomShopModel extends ShopModel {
    
    // "Has many" relationships return instances of PagiantedServiceModel.
    products!: PaginatedServiceModel<MyCustomProductModel>
    
}

Hooking into real-time events

If you're using the Feathers Socket.io client as described in the above setup instructions, you'll be able to listen for changes made to ATShop resources. This isn't strictly a feature of atshop-service-models as this functionality is provided directly by the Feathers instance you passed atshop-service-models during setup.

All model instances are equipped with a static service getter that returns a Feathers Service object specific to the model's associated service. This is where we derive the real-time functionality from in the below example.

import { OrderModel } from 'atshop-service-models';

// Do note that you will receive events for all orders that are created for shops you have administrative permissions for.
OrderModel.service.on('created', async (orderData) => {
    const order = new OrderModel(orderData);
    const product = await order.product;
    const stock = await product.stockForSale.count();
    
    console.log(`An order was created by ${order.email}, you'll have ${stock - order.quantity} stock left after the order has been paid for.`)
})

See the Feathers Events documentation for more information on this.

License

This repository is licensed under the ISC license.

Copyright (c) 2019, Jørgen Vatle.