Skip to content

Commit

Permalink
Issue #136 Add ES Module exports
Browse files Browse the repository at this point in the history
- Format all files per prettier
  • Loading branch information
kernwig committed Jan 29, 2025
1 parent 404bfa4 commit d195403
Show file tree
Hide file tree
Showing 31 changed files with 814 additions and 796 deletions.
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.serverless
.vscode
coverage
dist
node_modules
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,26 @@ Every tool is the genesis of real world needs, and they continue to evolve.
This collection is part of Rackspace Technology's commitment to give back to the open source community.
Find this and other Rackspace open source repositories on [GitHub](https://github.com/rackspace).


## Content

Each utility is described on its own page:

* [AwsHttps - HTTPS client with AWS Signature v4](docs/aws_https.md)
* [ElasticsearchClient - Communicate with AWS Elasticsearch](docs/elasticsearch_client.md)
* [ExpiringValue - Value that is instantiated on-demand and cached for a limited time](docs/expiring_value.md)
* [Injector - Light-weight and type-safe Dependency Injection](docs/injector.md)
* [LambdaUtils - AWS Lambda handler middleware](docs/lambda_utils.md)
* [Logger - CloudWatch and serverless-offline friendly logger](docs/logger.md)
* [StateStorage - Serverless state and configuration storage](docs/state_storage.md)
* [More Examples](docs/examples.md)
* [License](docs/license.md)
- [AwsHttps - HTTPS client with AWS Signature v4](docs/aws_https.md)
- [ElasticsearchClient - Communicate with AWS Elasticsearch](docs/elasticsearch_client.md)
- [ExpiringValue - Value that is instantiated on-demand and cached for a limited time](docs/expiring_value.md)
- [Injector - Light-weight and type-safe Dependency Injection](docs/injector.md)
- [LambdaUtils - AWS Lambda handler middleware](docs/lambda_utils.md)
- [Logger - CloudWatch and serverless-offline friendly logger](docs/logger.md)
- [StateStorage - Serverless state and configuration storage](docs/state_storage.md)
- [More Examples](docs/examples.md)
- [License](docs/license.md)

## Why "Sailplane"?

Onica's early OSS releases have had aviation themed names;
this may or may not have something to do with the CTO being a pilot. Nobody really knows.

Sailplane was selected for this *serverless* toolset by considering that
Sailplane was selected for this _serverless_ toolset by considering that
serverless is to computing without the complexities of a server,
as a sailplane is to flight without the complexities of an airplane.

Expand Down
5 changes: 4 additions & 1 deletion aws-https/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# @sailplane/aws-http - HTTPS client with AWS Signature v4

## What?
The AwsHttps class is an HTTPS (notice, *not* HTTP) client purpose made for use in and with AWS environments.

The AwsHttps class is an HTTPS (notice, _not_ HTTP) client purpose made for use in and with AWS environments.

This is part of the [sailplane](https://github.com/rackspace/sailplane) library of
utilities for AWS Serverless in Node.js.

## Why?

- Simple Promise or async syntax
- Optionally authenticates to AWS via AWS Signature v4 using [aws4](https://www.npmjs.com/package/aws4)
- Familiar [options](https://nodejs.org/api/http.html#http_http_request_options_callback>)
Expand All @@ -15,4 +17,5 @@ utilities for AWS Serverless in Node.js.
- Easily extended for unit testing

## How?

See the [docs](https://github.com/rackspace/sailplane/blob/master/README.md) for usage and examples.
81 changes: 40 additions & 41 deletions docs/aws_https.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ HTTPS client with AWS Signature v4.

## Overview

The AwsHttps class is an HTTPS (notice, *not* HTTP) client purpose made for use in and with AWS environments.
The AwsHttps class is an HTTPS (notice, _not_ HTTP) client purpose made for use in and with AWS environments.

- Simple Promise or async syntax
- Optionally authenticates to AWS via AWS Signature v4 using [aws4](https://www.npmjs.com/package/aws4)
Expand All @@ -30,11 +30,11 @@ npm install @sailplane/aws-https @sailplane/logger
Simple example to GET from URL:

```ts
const url = new URL('https://www.rackspace.com/ping.json');
const url = new URL("https://www.rackspace.com/ping.json");
const http = new AwsHttps();

// Build request options from a method and URL
const options = http.buildOptions('GET', url);
const options = http.buildOptions("GET", url);

// Make request and parse JSON response.
const ping = await http.request(options);
Expand All @@ -45,34 +45,33 @@ Example hitting API with the container's AWS credentials:
```ts
const awsHttp = new AwsHttps();
const options: AwsHttpsOptions = {
// Same options as https://nodejs.org/api/http.html#http_http_request_options_callback
method: 'GET',
hostname: apiEndpoint,
path: '/cloud-help',
headers: {
'accept': 'application/json; charset=utf-8',
'content-type': 'application/json; charset=utf-8'
},
timeout: 10000,

// Additional option for POST, PUT, or PATCH:
body: JSON.stringify({ website: "https://www.rackspace.com" }),

// Additional option to apply AWS Signature v4
awsSign: true
// Same options as https://nodejs.org/api/http.html#http_http_request_options_callback
method: "GET",
hostname: apiEndpoint,
path: "/cloud-help",
headers: {
accept: "application/json; charset=utf-8",
"content-type": "application/json; charset=utf-8",
},
timeout: 10000,

// Additional option for POST, PUT, or PATCH:
body: JSON.stringify({ website: "https://www.rackspace.com" }),

// Additional option to apply AWS Signature v4
awsSign: true,
};

try {
const responseObj = await awsHttp.request(options);
process(responseObj);
const responseObj = await awsHttp.request(options);
process(responseObj);
} catch (err) {
// HTTP status response is in statusCode field
if (err.statusCode === 404) {
process(undefined);
}
else {
throw err;
}
// HTTP status response is in statusCode field
if (err.statusCode === 404) {
process(undefined);
} else {
throw err;
}
}
```

Expand All @@ -83,15 +82,15 @@ Example hitting API with the custom AWS credentials:
const roleCredentials = await this.getAssumeRoleCredentials();

const awsCredentials = {
accessKey: roleCredentials.AccessKeyId,
secretKey: roleCredentials.SecretAccessKey,
sessionToken: roleCredentials.SessionToken,
accessKey: roleCredentials.AccessKeyId,
secretKey: roleCredentials.SecretAccessKey,
sessionToken: roleCredentials.SessionToken,
};
const http = new AwsHttps(false, awsCredentials);

// Build request options from a method and URL
const url = new URL('https://www.rackspace.com/ping.json');
const options = http.buildOptions('GET', url);
const url = new URL("https://www.rackspace.com/ping.json");
const options = http.buildOptions("GET", url);

// Make request and parse JSON response.
const ping = await http.request(options);
Expand All @@ -107,16 +106,16 @@ The Sailplane [ElasticsearchClient](elasticsearch_client.md) package is a simple

```ts
export class AwsHttpsFake extends AwsHttps {
constructor() {
super();
}
constructor() {
super();
}

async request(options: AwsHttpsOptions): Promise<any | null> {
// Check for expected options. Example:
expect(options.path).toEqual('/expected-path');
async request(options: AwsHttpsOptions): Promise<any | null> {
// Check for expected options. Example:
expect(options.path).toEqual("/expected-path");

// Return canned response
return Promise.resolve({ success: true });
}
// Return canned response
return Promise.resolve({ success: true });
}
}
```
5 changes: 3 additions & 2 deletions docs/elasticsearch_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ Simple example:

```ts
function get(id: string): Promise<Ticket> {
return this.es.request('GET', '/ticket/local/' + id)
.then((esDoc: ElasticsearchResult) => esDoc._source as Ticket);
return this.es
.request("GET", "/ticket/local/" + id)
.then((esDoc: ElasticsearchResult) => esDoc._source as Ticket);
}
```

Expand Down
22 changes: 12 additions & 10 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ This section includes some larger examples which use _multiple_ packages.

Uses:

* [AwsHttps](aws_https.md)
* [ElasticsearchClient](elasticsearch_client.md)
* [Injector](injector.md)
* [Logger](logger.md)
- [AwsHttps](aws_https.md)
- [ElasticsearchClient](elasticsearch_client.md)
- [Injector](injector.md)
- [Logger](logger.md)

```ts
import {AwsHttps} from "@sailplane/aws-https";
Expand Down Expand Up @@ -149,24 +149,26 @@ functions:
```ts
// src/handlers.ts
import 'source-map-support/register';
import {APIGatewayEvent} from 'aws-lambda';
import {Injector} from "@sailplane/injector";
import "source-map-support/register";
import { APIGatewayEvent } from "aws-lambda";
import { Injector } from "@sailplane/injector";
import * as LambdaUtils from "@sailplane/lambda-utils";
import {ChatService} from "./chat-service";
import { ChatService } from "./chat-service";
import * as createHttpError from "http-errors";

Injector.register(StateStorage, () => new StateStorage(process.env.STATE_STORAGE_PREFIX));

/**
* Fetch history of chat on the user's channel
*/
export const getChatHistory = LambdaUtils.wrapApiHandler(async (event: LambdaUtils.APIGatewayProxyEvent) => {
export const getChatHistory = LambdaUtils.wrapApiHandler(
async (event: LambdaUtils.APIGatewayProxyEvent) => {
const channel = event.queryStringParameters.channel;
const cursor = event.queryStringParameters.cursor;

return Injector.get(ChatService)!.getHistory(channel, cursor);
});
},
);
```

```ts
Expand Down
12 changes: 6 additions & 6 deletions docs/expiring_value.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ const https = new AwsHttps();
const cache = {};

export function fetchWithCache(url: string): Promise<any> {
if (!cache[url]) {
cache[url] = new ExpiringValue<any>(() => loadData(url), CACHE_PERIOD);
}
if (!cache[url]) {
cache[url] = new ExpiringValue<any>(() => loadData(url), CACHE_PERIOD);
}

return cache[url].get();
return cache[url].get();
}

function loadData(url: string): any {
const req = https.buildRequest('GET', new URL(url));
return https.request(req);
const req = https.buildRequest("GET", new URL(url));
return https.request(req);
}
```

Expand Down
Loading

0 comments on commit d195403

Please sign in to comment.