Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ts and update #1

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Unloop Rest API

This library contains utility functions needed by various REST API's.

They're divided as follows:

## http

Utility classes representing HTTP Status Codes and methods.
* **StatusCode**
* **HTTPMethod**

helper function for building URL's with query parameters
* **buildQueryParamUrl**

A thin wrapper around a phin client that takes in a bearer token and can be reused to perform authorized requests on the specified URL
* **authorizeGet**
* **authorizePost**
* **authorizePut**
* **authorizePatch**
* **authorizeDelete**

functions taking in a body object that returns a formatted HTTP response object with the appropriate status code and CORS headers. If a body is passed, it will automatically be json formatted. noContent() will throw an error if a body is passed.
* **success**
* **created**
* **noContent**

functions taking in a body object that returns a formatted HTTP response object with the appropriate status code and CORS headers. If an error message is passed, it will be automatically formatted into {message: ERROR_MESSAGE} and returned to the user.
* **failure**
* **notFound**
* **badRequest**

helper function to build json formatted HTTP responses, takes in a status code and returns a function taking in a body object. You shouldn't need to use this often as we provide sucess/created/noContent response wrappers.
* **buildResponse**

helper function to build json formatted HTTP error responses, takes in a status code and returns a function taking in an error message string that will be automatically wrapped in the body {message: ERROR_MESSAGE}. You shouldn't need to use this often as we provide failure/notFound/badRequest response wrappers.
* **buildErrorResponse**


## lambda

Helps to resolve lambda paths to resources with get/getAll/update/remove/post functions. Matches based on resource name.
* **resourceRouter**


## oauth

Assuming oauth credentials are stored in the expected format inside of AWS Parameter Store, provides the ability to retrieve both jwt and client credential tokens. See README inside of the oauth folder for additional details on setup and usage.

* **jwt**
* **clientCredentials**

## usage

This library can be imported and used inside of your project as follows:

Importing the entire library:

```
import * as lib from 'unloop-rest-api';

console.log('string get method', lib.HTTPMethod.GET);
console.log('failure response object', lib.failure('fail'));
```

As named imports:

```
import {http, lambda} from 'unloop-rest-api';

console.log('string get method', http.HTTPMethod.GET);
console.log('failure response object', lambda.resourceRouter(ROUTES));
```


```
import {HTTPMethod, resourceRouter} from 'unloop-rest-api';

console.log('string get method', HTTPMethod.GET);
console.log('failure response object', resourceRouter(ROUTES));
```
12 changes: 11 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
export * from './src';
export * as oauth from './src/oauth';
export * from './src/oauth';

export * as http from './src/http';
export * from './src/http/httpMethods';
export * from './src/http/buildResponse';
export * from './src/http/requests';
export * from './src/http/statusCodes';

export * as lambda from './src/lambda';
export * from './src/lambda/resourceRouter';
Loading