Skip to content

Commit

Permalink
feat(nodejs): adds SSR support (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
jshor committed Apr 29, 2020
1 parent 14d44b7 commit e7d8ea5
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/ICalendar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import CalendarBase from './CalendarBase'
import { formatText, getUid, getRrule, download } from './utils/ics'
import { formatText, getUid, getProdId, getRrule, download } from './utils/ics'
import { getTimeCreated } from './utils/time'

/**
Expand Down Expand Up @@ -52,6 +52,8 @@ export default class ICalendar extends CalendarBase {

/**
* Downloads the rendered iCalendar.
*
* @note Only works in browsers.
*/
download () {
download(this.title, this.render())
Expand Down Expand Up @@ -82,7 +84,7 @@ export default class ICalendar extends CalendarBase {

const uid = getUid()
const timeCreated = getTimeCreated()
const host = window.location.host
const host = getProdId()
const calendar = [
'BEGIN:VCALENDAR',
'VERSION:2.0',
Expand Down
12 changes: 2 additions & 10 deletions src/__tests__/ICalendar.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import moment from 'moment'
import { FORMAT } from '../constants'
import { formatText, getUid, getRrule, download } from '../utils/ics'
import { formatText, getUid, getProdId, download } from '../utils/ics'
import { getTimeCreated } from '../utils/time'
import CalendarBase from '../CalendarBase'
import ICalendar from '../ICalendar'
Expand Down Expand Up @@ -52,15 +52,7 @@ describe('ICalendar', () => {
beforeEach(() => {
formatText.mockImplementation((...args) => args.join(' formatted '))
getUid.mockReturnValue(24)

Object.defineProperty(global, 'window', {
value: {
location: {
host: 'foobar',
},
},
writable: true,
})
getProdId.mockReturnValue('foobar')
})

afterEach(() => {
Expand Down
25 changes: 25 additions & 0 deletions src/utils/__tests__/ics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getUid,
getRrule,
download,
getProdId,
} from '../ics'
import { formatTimestampString } from '../time'
import FileSaver from 'file-saver';
Expand Down Expand Up @@ -111,6 +112,30 @@ describe('IcsUtil', () => {
})
})

describe('getProdId()', () => {
it('should return `datebook` in a non-browser context', () => {
Object.defineProperty(global, 'window', {
value: undefined,
writable: true
})

expect(getProdId()).toEqual('datebook')
})

it('should return the window host in the browser context', () => {
const host = 'mydomain.com'

Object.defineProperty(global, 'window', {
value: {
location: { host }
},
writable: true
})

expect(getProdId()).toEqual(host)
})
})

describe('getRrule()', () => {
it('should transform the object into an ICS param string', () => {
const recurrence = {
Expand Down
13 changes: 12 additions & 1 deletion src/utils/ics.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ export const getUid = () => {
return Math.random().toString(36).substr(2)
}

/**
* Returns the hostname for usage in `PRODID`. Returns `datebook` in Node.js.
*
* @returns {String}
*/
export const getProdId = () => {
return typeof window !== 'undefined'
? window.location.host
: 'datebook'
}

/**
* Converts the given recurrence options to RFC????
*
Expand Down Expand Up @@ -105,4 +116,4 @@ export const download = (title, data) => {
} else {
FileSaver.saveAs(getBlob(data), fileName)
}
}
}
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module.exports = {
library: 'datebook',
libraryTarget: 'umd'
},
target: 'node',
resolve: {
extensions: ['.js'],
},
Expand Down

0 comments on commit e7d8ea5

Please sign in to comment.