Skip to content

Commit

Permalink
Open-sourcing electrode-cookies
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Aug 6, 2017
1 parent 0d7942a commit d918552
Show file tree
Hide file tree
Showing 15 changed files with 1,077 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/electrode-cookies/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage
node_modules
3 changes: 3 additions & 0 deletions packages/electrode-cookies/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
extends:
- "./node_modules/electrode-archetype-njs-module-dev/config/eslint/.eslintrc-node"
226 changes: 226 additions & 0 deletions packages/electrode-cookies/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@

# Created by https://www.gitignore.io/api/osx,node

### OSX ###
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Node ###
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://docs.npmjs.com/misc/faq#should-i-check-my-node-modules-folder-into-git
node_modules

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

### Vim template
[._]*.s[a-w][a-z]
[._]s[a-w][a-z]
*.un~
Session.vim
.netrwhist
*~
### GitBook template
# Node rules:
## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

## Dependency directory
## Commenting this out is preferred by some people, see
## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git
node_modules

# Book build output
_book

# eBook build output
*.epub
*.mobi
*.pdf
### TortoiseGit template
# Project-level settings
/.tgitconfig
### Xcode template
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
### Emacs template
# -*- mode: gitignore; -*-
*~
\#*\#
/.emacs.desktop
/.emacs.desktop.lock
*.elc
auto-save-list
tramp
.\#*

# Org-mode
.org-id-locations
*_archive

# flymake-mode
*_flymake.*

# eshell files
/eshell/history
/eshell/lastdir

# elpa packages
/elpa/

# reftex files
*.rel

# AUCTeX auto folder
/auto/

# cask packages
.cask/
### OSX template
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
/clap.js
/.npmignore
.nyc_output
112 changes: 112 additions & 0 deletions packages/electrode-cookies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Electrode Cookies

Electrode isomorphic cookies lib.

## Install

npm install electrode-cookies --save

## Usage

This module offers reading and setting cookies in React code that works in both the browser or when doing Server Side Rendering.

In your pure server only code, you can also use this module to read and set cookies, but you **MUST** pass the `request` object in the options. Otherwise an assert error will be thrown.

In NodeJS land:

```js
const Cookies = require("electrode-cookies");
```

### Reading cookies

In ReactJS land:

```js
import Cookies from "electrode-cookies";
const value = Cookies.get("test-cookie");
```

In NodeJS land:

> Note the difference is that `request` is passed in options.
```js
const Cookies = require("electrode-cookies");
const value = Cookies.get("test-cookie", { request });
```

### Writing cookies

In ReactJS land:

```js
import Cookies from "electrode-cookies";
Cookies.set( "foo", "bar", { path: "/", domain: ".walmart.com" } );
```

In NodeJS land:

> Note the difference is that `request` is passed in options.
```js
const Cookies = require("electrode-cookies");
Cookies.set( "foo", "bar", { request, path: "/", domain: ".walmart.com" } );
```

## Electrode Server Setup

The cookie writing on server side requires support from a Hapi plugin. If you use [electrode-server], then it should have setup the plugin for you by default. Otherwise, you need to register the [hapi plugin](hapi-plugin.js).

## APIs

### [Cookies.get](#cookiesget)

`Cookies.get(key, [options])`

Parameters:

- `key` - name of the cookie
- `options` - (optional) **_Available for Server side only._** options for getting the cookie
- `request` - The server `request` object (**Required on server**).
- `matchSubStr` - If `true`, then do substring matching of key with all cookie keys.
- `skipEncoding` - (applies only if `matchSubStr` is `true`) If `true`, then do not encode the key or decode the value.

Returns the value of the cookie for `key`.

### [Cookies.set](#cookiesset)

`Cookies.set(key, value, [options])`

Set a cookie with `key` and `value`.

Parameters:

- `key` - name of the cookie
- `value` - value of the cookie
- `options` - (optional) options for the cookie
- `request` - On the server side, the `request` object (**Required on server**).
- `path` - string path of the cookie **_Default:_** `"/"`
- `domain` - string domain of the cookie
- `expires` - number of seconds the cookie will expire
- `secure` - A boolean of whether or not the cookie should only be available over SSL **_Default:_** false
- `httpOnly` - A boolean of whether or not the cookie should only be available over HTTP(S) **_Default:_** false
- `forceAuthEncoding` - Forces non-standard encoding for `+` and `/` characters, use with auth cookies.
- `skipEncoding` - Skip encoding/escaping of the cookie value. See [source](https://gecgithub01.walmart.com/electrode/electrode-cookies/blob/master/lib/index.js) for details.

### [Cookies.expire](#cookiesexpire)

`Cookies.expire(key, [options])`

Expires a cookie specified by `key`.

Parameters:

- `key` - name of the cookie
- `options` - (optional) options for the cookie
- `path` - string path of the cookie **_Default:_** `"/"`
- `domain` - string domain of the cookie
- `secure` - A boolean of whether or not the cookie should only be available over SSL **_Default:_** false
- `request` - The server request object (**Required on server**)

[electrode-server]: https://gecgithub01.walmart.com/electrode/electrode-server
Loading

0 comments on commit d918552

Please sign in to comment.