@djabry/aws-factory
TypeScript icon, indicating that this package has built-in type declarations

0.0.23 • Public • Published

AWS factory

A library for simplifying your authentication and class instantiation logic when working with the AWS Javascript SDK.

Why?

Many of the projects I've worked on involved a step requiring a user to authenticate prior to accessing AWS resources. I found that writing a centralised utility for instantiating the various AWS classes after loading the required credentials simplified other dependent parts of my applications by separating concerns.

How?

  1. Install the library:

    npm i @djabry/aws-factory
  2. Initialise your factory:

    import {AwsServiceFactory} from "@djabry/aws-factory";
    const awsFactory = new AwsServiceFactory();
  3. In some part of your application that doesn't care about credentials:

    import * as S3 from "aws-sdk/clients/s3";
    
    async function loadThing() {
       const s3 = await awsFactory.getService(S3);
       const data = await s3.getObject({Bucket: "foo", Key: "bar"}).promise();
       return data.Body.toString();
    }
    
    loadThing().then(thing => console.log("Thing loaded:", thing));
  4. Authenticate later on in some other part of your application:

    import {Credentials} from "aws-sdk/lib/credentials";
    awsFactory.credentials = new Credentials("foo", "bar");

    In your console:

    Thing loaded: baz

What?

This projects contains some utilities for instantiating AWS classes in a Node/browser environment after a user has successfully authenticated. Service instances are cached so that each dependent part of your application is given the same instance when using the getService method.

Configuration

You can configure the service using the optional second argument of the getService method e.g.

const s3 = await awsFactory.getService(S3, {apiVersion: '2006-03-01'});

/@djabry/aws-factory/

    Package Sidebar

    Install

    npm i @djabry/aws-factory

    Weekly Downloads

    2

    Version

    0.0.23

    License

    MIT

    Unpacked Size

    21 kB

    Total Files

    31

    Last publish

    Collaborators

    • djabry