4.1.7 • Published 6 days ago

atshop-service-models v4.1.7

Weekly downloads
290
License
ISC
Repository
github
Last release
6 days ago

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.

4.1.7

6 days ago

4.1.6

6 days ago

4.1.5

22 days ago

4.1.4

1 month ago

4.1.3

1 month ago

4.1.2

2 months ago

4.1.0

3 months ago

4.1.1

3 months ago

4.0.5

6 months ago

4.0.4

6 months ago

4.0.7

6 months ago

4.0.6

6 months ago

4.0.3

6 months ago

4.0.1

1 year ago

4.0.2

1 year ago

4.0.0

1 year ago

3.0.8

2 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

2.0.1

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.1.2

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.39.11

3 years ago

0.39.12

3 years ago

1.0.3

3 years ago

0.39.10

3 years ago

0.39.9

3 years ago

0.39.8

3 years ago

0.39.7

3 years ago

0.39.6

3 years ago

0.39.5

3 years ago

0.39.4

3 years ago

0.39.3

3 years ago

0.39.2

3 years ago

0.39.1

3 years ago

0.39.0

3 years ago

0.38.4

4 years ago

0.38.3

4 years ago

0.38.2

4 years ago

0.38.1

4 years ago

0.38.0

4 years ago

0.37.5

4 years ago

0.37.4

4 years ago

0.37.3

4 years ago

0.37.2

4 years ago

0.37.1

4 years ago

0.37.0

4 years ago

0.36.4

4 years ago

0.36.3

4 years ago

0.36.2

4 years ago

0.36.1

4 years ago

0.36.0

4 years ago

0.35.0

4 years ago

0.34.3

4 years ago

0.34.2

4 years ago

0.34.1

4 years ago

0.34.0

4 years ago

0.33.0

4 years ago

0.32.2

4 years ago

0.32.1

4 years ago

0.32.0

4 years ago

0.31.1

5 years ago

0.31.0

5 years ago

0.30.1

5 years ago

0.30.0

5 years ago

0.29.0

5 years ago

0.28.0

5 years ago

0.28.0-rc2

5 years ago

0.28.0-rc1

5 years ago

0.27.0

5 years ago

0.26.2

5 years ago

0.26.1

5 years ago

0.26.0

5 years ago

0.25.0

5 years ago

0.24.0

5 years ago

0.23.0

5 years ago

0.22.1

5 years ago

0.22.0

5 years ago

0.21.1

5 years ago

0.21.0

5 years ago

0.20.2

5 years ago

0.20.0

5 years ago

0.19.1

5 years ago

0.19.0

5 years ago

0.18.0

5 years ago

0.17.0

5 years ago

0.16.1

5 years ago

0.16.0

5 years ago

0.15.2

5 years ago

0.15.0

5 years ago

0.14.0

5 years ago

0.13.0

5 years ago

0.12.1

5 years ago

0.12.0

5 years ago

0.11.0

5 years ago

0.10.0

5 years ago

0.9.0

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.0

5 years ago

0.5.0

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago