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

@architect-io/sdk

v0.2.2

Published

Node.js SDK for Architect.io

Downloads

9

Readme

Architect Node.js SDK

This is the Node.js SDK used for brokering connections to microservice dependencies via Architect's deployment platform. If you're unfamiliar with the platform or our deploy tools, please check out Architect.io and our CLI to get started.

Installation

$ npm i @architect-io/sdk --save

SDK Documentation

Connecting to dependencies

import architect from '@architect-io/sdk';

const my_dependency = architect.service('my_dependency_name');

// Client used to connect to service
const client = my_dependency.client;

// Dynamically imported models used for message formatting
// Used by protocols supporting code-gen like gRPC
const defs = my_dependency.defs;

REST

The Architect SDK uses the popular REST client, Axios, to broker communication to downstream REST microservices. The client that is provided is an Axios instance that is enriched with the proper service location meaning only URIs and HTTP actions need be provided:

const axios_res = await my_dependency.client.get('/resource');
const axios_res = await my_dependency.client.post('/resource', { data });
const axios_res = await my_dependency.client.put('/resource/:resource_id', { data });
const axios_res = await my_dependency.client.delete('/resource/:resource_id');

gRPC

Architect handles the code-gen for gRPC services for you. Every time you run $ architect install ..., you'll find relevant gRPC stubs generated and placed inside an architect_services folder in your service's root directory. This SDK handles the import of generated model objects for parsing input/output messages as well as the import and enrichment of client code for making calls to dependencies.

// example.proto
syntax = "proto3";

package example_service;

message PayRequest {
    int32 amount = 1;
}

message PayResponse {
    int32 output = 1;
}

service Example {
  rpc Pay (PayRequest) returns (PayResponse) {}
}
// Model definitions will match message names in the .proto file for the service
const pay_req = my_dependency.defs.PayRequest();
pay_req.setAmount(10);

// Client will automatically have methods matching the service definition from 
// the dependency's .proto file. Response handling matches gRPC documentation.
my_dependency.client.pay(pay_req, (error, grpc_res) => {
  const output = grpc_res.getOutput();
});

Connecting to data stores

Since there are so many DB clients available, we don't want to choose a default for developers. Instead, the Architect SDK provides easy mechanics to parse credentials provided by the platform:

import architect from '@architect-io/sdk';
import {createConnection} from "typeorm";

// Key used to cite the datastore must match the key used in your 
// architect service config
const db_config = architect.datastore('primary_db');
const connection = await createConnection({
   type: "postgres",
   host: db_config.host,
   port: db_config.port,
   username: db_config.username,
   password: db_config.password,
   database: db_config.name
});

Events and messaging

The Architect platform also supports pub/sub based communication between services. The primary use-case for this flow would be to allow services to broadcast events for other services to subscribe to without needing to know who the subscribers are.

NOTE: As of v0.2.x of the SDK, the only method for publishing/subscribing to events using Architect is via REST (mirroring the function of traditional webhooks). We're actively working on means of supporting queuing systems like RabbitMQ, AWS SQS, and more.

Subscribing

// architect.json
{
  "subscriptions": {
    "<service_name>": {
      "<event_name>": {
        "uri": "/event/callback",
        "headers": {
          "x-custom-header": "example"
        }
      }
    }
  }
}

The URI used for registration must match a URI on your service:

import * as express from 'express';
import architect from '@architect-io/sdk';

const app = express();

app.post('/event/callback', (req, res) => {
  // Custom event handling here...
});

app.listen(process.env.PORT);

Publishing

NOTE: simple publication methods coming soon. For now, you can iterate through subscribers to submit events.

// architect.json
{
  "notifications": ["<event_name>"]
}
import * as axios from 'axios';
import architect from '@architect-io/sdk';

// Iterate through notification subscribers to POST payload
architect.notification('<event_name>').subscriptions.forEach(async (sub) => {
  const axios_instance = axios.create({
    baseURI: `${sub.host}:${sub.port}`,
    headers: sub.headers
  });
  await axios_instance.post(sub.uri, { payload });
});