-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial commit of the
nylas-react
files
- Loading branch information
1 parent
23d08c5
commit 47307cb
Showing
7 changed files
with
178 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,57 @@ | ||
# StandardTemplate | ||
Standard template with branch protections | ||
# Nylas React SDK | ||
|
||
![npm](https://img.shields.io/npm/v/@nylas/nylas-react) | ||
|
||
This is the GitHub repository for the Nylas React SDK. The Nylas Communications Platform provides REST APIs for [Email](https://developer.nylas.com/docs/connectivity/email/), [Calendar](https://developer.nylas.com/docs/connectivity/calendar/), and [Contacts](https://developer.nylas.com/docs/connectivity/contacts/), and the Node SDK is the quickest way to build your integration using React. | ||
|
||
Here are some resources to help you get started: | ||
|
||
- [Quickstart](https://developer.nylas.com/docs/the-basics/quickstart/) | ||
- [Nylas API Reference](https://developer.nylas.com/docs/api/) | ||
|
||
|
||
# Install | ||
|
||
To install the Nylas React SDK, you will first need to have [npm](https://www.npmjs.com/get-npm) installed on your machine. | ||
|
||
Then, head to the nearest command line and run the following: | ||
```bash | ||
npm install @nylas/nylas-react | ||
``` | ||
|
||
To install this package from source, clone this repo and run `npm install` from inside the project directory: | ||
|
||
```bash | ||
git clone https://github.com/nylas/nylas-react.git | ||
cd nylas-react | ||
npm install | ||
``` | ||
|
||
# Usage | ||
|
||
The Nylas React SDK provides an easy way to implement authentication in your react application. | ||
|
||
### Components | ||
|
||
The Nylas React SDK provides the following component: | ||
|
||
* [NylasContainer](src/nylas-container.tsx) - This is a component that utilizes React Context API to maintain a state for authentication and the [Nylas JS](https://github.com/nylas/nylas-js) client. This context can be accessed via the [useNylas](https://github.com/nylas/nylas-react#useNylas) hook. | ||
|
||
### Hooks | ||
These are the following options that can be passed in to configure an instance of the Nylas JS SDK | ||
|
||
* useNylas - returns an object with the following: | ||
* client - The Nylas JS client instance | ||
* authState - The current authentication state | ||
* authWithRedirect - The function for building, and redirecting to, the authentication URL | ||
* exchangeCodeFromUrlForToken - The function for parsing the code from the authentication URL and exchanging it for an access token | ||
|
||
# Contributing | ||
|
||
Please refer to [Contributing](Contributing.md) for information about how to make contributions to this project. We welcome questions, bug reports, and pull requests. | ||
|
||
# License | ||
|
||
This project is licensed under the terms of the MIT license. Please refer to [LICENSE](LICENSE.txt) for the full terms. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@nylas/nylas-react", | ||
"version": "0.1.0", | ||
"description": "React SDK for the Nylas Platform API", | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts", | ||
"scripts": { | ||
"build": "tsc" | ||
}, | ||
"dependencies": { | ||
"@nylas/nylas-js": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^4.7.4", | ||
"react": "^18.2.0", | ||
"@types/react": "^18.0.12" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/nylas/nylas-react.git" | ||
}, | ||
"keywords": [ | ||
"email", | ||
"contacts", | ||
"calendar", | ||
"nylas" | ||
], | ||
"author": "Nylas, Inc.", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/nylas/nylas-react/issues" | ||
}, | ||
"homepage": "https://github.com/nylas/nylas-react#readme" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export { | ||
default as NylasContainer, | ||
NylasContainerOptions | ||
} from './nylas-container'; | ||
export { | ||
default as NylasContext, | ||
NylasContextInterface, | ||
useNylas | ||
} from './nylas-context'; | ||
export { | ||
NylasProps, | ||
AuthUrlOptions, | ||
ExchangeCodeOptions | ||
} from '@nylas/nylas-js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import React, { useCallback, useState } from "react"; | ||
import NylasContext from "./nylas-context"; | ||
import Nylas, { AuthUrlOptions, ExchangeCodeOptions } from "@nylas/nylas-js"; | ||
|
||
export interface NylasContainerOptions { | ||
serverBaseUrl: string; | ||
children?: React.ReactNode; | ||
} | ||
|
||
const NylasContainer = (opts: NylasContainerOptions): JSX.Element => { | ||
const {children, ...nylasProps} = opts; | ||
const [client] = useState(() => new Nylas(nylasProps)); | ||
const [authState, setAuthState] = useState(false); | ||
|
||
const authWithRedirect = useCallback( | ||
async (opts: AuthUrlOptions): Promise<void|boolean> => client.authWithRedirect(opts), | ||
[client] | ||
); | ||
|
||
const exchangeCodeFromUrlForToken = useCallback( | ||
async (opts?: ExchangeCodeOptions): Promise<string | boolean> => { | ||
const accessToken = await client.exchangeCodeFromUrlForToken(opts); | ||
if(accessToken !== false) { | ||
setAuthState(true); | ||
} | ||
|
||
return accessToken; | ||
}, | ||
[client, authState] | ||
) | ||
|
||
return ( | ||
<NylasContext.Provider value={{ | ||
client, | ||
authState, | ||
authWithRedirect, | ||
exchangeCodeFromUrlForToken | ||
}}> | ||
{children} | ||
</NylasContext.Provider> | ||
) | ||
} | ||
|
||
export default NylasContainer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as React from 'react'; | ||
import Nylas, {AuthUrlOptions, ExchangeCodeOptions} from "@nylas/nylas-js"; | ||
|
||
export interface NylasContextInterface { | ||
client: Nylas; | ||
authState: boolean; | ||
authWithRedirect(opts: AuthUrlOptions): Promise<void|boolean>; | ||
exchangeCodeFromUrlForToken(opts?: ExchangeCodeOptions): Promise<string | boolean>; | ||
} | ||
|
||
const NylasContext = React.createContext<NylasContextInterface | null>(null); | ||
|
||
export const useNylas = (): NylasContextInterface => React.useContext(NylasContext) as NylasContextInterface; | ||
|
||
export default NylasContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es6", | ||
"module": "es2022", | ||
"outDir": "./lib", | ||
"jsx": "react", | ||
"moduleResolution": "node", | ||
"declaration": true, | ||
"allowSyntheticDefaultImports": true | ||
} | ||
} |