Skip to content

Commit 50a5bef

Browse files
authored
Merge pull request #75 from nono/m4dz-chore/umd
Build UMD modules in root['cozy']
2 parents 094df46 + e3df606 commit 50a5bef

File tree

14 files changed

+253
-255
lines changed

14 files changed

+253
-255
lines changed

src/index.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* global fetch */
2+
23
import {unpromiser, retry, warn} from './utils'
34
import {LocalStorage, MemoryStorage} from './auth_storage'
45
import {AppToken as AppTokenV2, getAppToken as getAppTokenV2} from './auth_v2'
@@ -92,7 +93,7 @@ const settingsProto = {
9293
diskUsage: settings.diskUsage
9394
}
9495

95-
class Cozy {
96+
class Client {
9697
constructor (options) {
9798
this.data = {}
9899
this.files = {}
@@ -246,12 +247,5 @@ function addToProto (ctx, obj, proto, disablePromises) {
246247
}
247248
}
248249

249-
const cozy = new Cozy()
250-
251-
export default cozy
252-
export { Cozy, LocalStorage, MemoryStorage }
253-
254-
if ((typeof window) !== 'undefined') {
255-
window.cozy = cozy
256-
window.Cozy = Cozy
257-
}
250+
module.exports = new Client()
251+
Object.assign(module.exports, {Client, LocalStorage, MemoryStorage})

test/integration/data.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// eslint-disable-next-line no-unused-vars
44
import should from 'should'
55
import 'isomorphic-fetch'
6-
import {Cozy} from '../../src'
6+
import {Client} from '../../src'
77
import mockTokenRetrieve from '../mock-iframe-token'
88

99
const COZY_STACK_URL = process.env && process.env.COZY_STACK_URL || ''
@@ -13,14 +13,14 @@ const COZY_STACK_TOKEN = process.env && process.env.COZY_STACK_TOKEN
1313
describe('data API', function () {
1414
let docID = null
1515
let docRev = null
16-
let cozy
16+
const cozy = {}
1717

1818
if (COZY_STACK_VERSION === '2') {
1919
before(mockTokenRetrieve)
2020
}
2121

2222
beforeEach(() => {
23-
cozy = new Cozy({
23+
cozy.client = new Client({
2424
cozyURL: COZY_STACK_URL,
2525
token: COZY_STACK_TOKEN
2626
})
@@ -31,7 +31,7 @@ describe('data API', function () {
3131
const testDoc = {
3232
'test': 'value'
3333
}
34-
const created = await cozy.data.create('io.cozy.testobject', testDoc)
34+
const created = await cozy.client.data.create('io.cozy.testobject', testDoc)
3535
created.should.have.property('_id')
3636
created.should.have.property('_rev')
3737
created.should.have.property('test', 'value')
@@ -42,7 +42,7 @@ describe('data API', function () {
4242

4343
describe('Fetch document', function () {
4444
it('Works', async function () {
45-
let fetched = await cozy.data.find('io.cozy.testobject', docID)
45+
let fetched = await cozy.client.data.find('io.cozy.testobject', docID)
4646
fetched.should.have.property('_id', docID)
4747
fetched.should.have.property('_rev', docRev)
4848
fetched.should.have.property('test', 'value')
@@ -54,7 +54,7 @@ describe('data API', function () {
5454
const changes = {
5555
'test': 'value2'
5656
}
57-
const updated = await cozy.data.update('io.cozy.testobject', { _id: docID, _rev: docRev }, changes)
57+
const updated = await cozy.client.data.update('io.cozy.testobject', { _id: docID, _rev: docRev }, changes)
5858
updated.should.have.property('_id', docID)
5959
updated.should.have.property('_rev')
6060
updated.should.have.property('test', 'value2')
@@ -65,7 +65,7 @@ describe('data API', function () {
6565

6666
describe('Delete document', function () {
6767
it('Works', async function () {
68-
const deleted = await cozy.data.delete('io.cozy.testobject', { _id: docID, _rev: docRev })
68+
const deleted = await cozy.client.data.delete('io.cozy.testobject', { _id: docID, _rev: docRev })
6969
deleted.should.have.property('id', docID)
7070
deleted.should.have.property('rev')
7171
})

test/integration/files.js

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import should from 'should'
66
import 'isomorphic-fetch'
77
import { Readable } from 'stream'
8-
import {Cozy} from '../../src'
8+
import {Client} from '../../src'
99
import {randomGenerator} from '../helpers'
1010

1111
const COZY_STACK_URL = process.env && process.env.COZY_STACK_URL || ''
@@ -14,13 +14,13 @@ const COZY_STACK_TOKEN = process.env && process.env.COZY_STACK_TOKEN
1414

1515
describe('files API', async function () {
1616
let random
17-
let cozy
17+
const cozy = {}
1818

1919
beforeEach(function () {
2020
if (COZY_STACK_VERSION === '2') {
2121
this.skip()
2222
}
23-
cozy = new Cozy({
23+
cozy.client = new Client({
2424
cozyURL: COZY_STACK_URL,
2525
token: COZY_STACK_TOKEN
2626
})
@@ -31,7 +31,7 @@ describe('files API', async function () {
3131
const filename = 'foo_' + random()
3232
const date = new Date('Wed, 01 Feb 2017 10:24:42 GMT')
3333

34-
const created = await cozy.files.create('datastring1', {
34+
const created = await cozy.client.files.create('datastring1', {
3535
name: filename,
3636
contentType: 'application/json',
3737
lastModifiedDate: date
@@ -50,7 +50,7 @@ describe('files API', async function () {
5050
stream.push('datastring1')
5151
stream.push(null)
5252

53-
const created = await cozy.files.create(stream, {
53+
const created = await cozy.client.files.create(stream, {
5454
name: filename,
5555
contentType: 'application/json'
5656
})
@@ -62,34 +62,34 @@ describe('files API', async function () {
6262
it('updates a file', async function () {
6363
const filename = 'foo_' + random()
6464

65-
const created = await cozy.files.create('datastring1', { name: filename })
65+
const created = await cozy.client.files.create('datastring1', { name: filename })
6666
created.should.have.property('attributes')
6767

6868
const createdId = created._id
6969

70-
const updated = await cozy.files.updateById(createdId, 'datastring2')
70+
const updated = await cozy.client.files.updateById(createdId, 'datastring2')
7171
updated.should.have.property('attributes')
7272
updated.attributes.md5sum.should.equal('iWpp8tcTP/DWTJSLf0hoyQ==')
7373
})
7474

7575
it('updates attributes', async function () {
7676
const filename = 'foo_' + random()
7777

78-
const created = await cozy.files.create('datastring1', { name: filename })
78+
const created = await cozy.client.files.create('datastring1', { name: filename })
7979
created.should.have.property('attributes')
8080

8181
const createdId = created._id
8282

8383
const newname1 = 'newname1_' + random()
8484
const attrs1 = { tags: ['foo', 'bar'], name: newname1 }
85-
const updated1 = await cozy.files.updateAttributesById(createdId, attrs1)
85+
const updated1 = await cozy.client.files.updateAttributesById(createdId, attrs1)
8686
updated1.should.have.property('attributes')
8787
updated1.attributes.name.should.startWith('newname1_')
8888
updated1.attributes.tags.should.eql(['foo', 'bar'])
8989

9090
const newname2 = 'newname2_' + random()
9191
const attrs2 = { tags: ['foo'], name: newname2 }
92-
const updated2 = await cozy.files.updateAttributesByPath('/' + newname1, attrs2)
92+
const updated2 = await cozy.client.files.updateAttributesByPath('/' + newname1, attrs2)
9393
updated2.should.have.property('attributes')
9494
updated2.attributes.name.should.startWith('newname2_')
9595
updated2.attributes.tags.should.eql(['foo'])
@@ -99,7 +99,7 @@ describe('files API', async function () {
9999
const dirname = 'foo_' + random()
100100
const date = new Date('Wed, 01 Feb 2017 10:24:42 GMT')
101101

102-
const created = await cozy.files.createDirectory({ name: dirname, lastModifiedDate: date })
102+
const created = await cozy.client.files.createDirectory({ name: dirname, lastModifiedDate: date })
103103
created.should.have.property('attributes')
104104
new Date(created.attributes.created_at).should.eql(date)
105105
new Date(created.attributes.updated_at).should.eql(date)
@@ -108,66 +108,66 @@ describe('files API', async function () {
108108
it('gets directory info by ID', async function () {
109109
const dirname = 'foo_' + random()
110110

111-
const created = await cozy.files.createDirectory({ name: dirname })
111+
const created = await cozy.client.files.createDirectory({ name: dirname })
112112
let directoryID = created._id
113113

114-
const stats = await cozy.files.statById(directoryID)
114+
const stats = await cozy.client.files.statById(directoryID)
115115
stats.should.have.property('attributes')
116116
})
117117

118118
it('gets directory info by Path', async function () {
119119
const dirname = 'foo_' + random()
120120

121-
const created = await cozy.files.createDirectory({ name: dirname })
121+
const created = await cozy.client.files.createDirectory({ name: dirname })
122122

123-
const stats = await cozy.files.statByPath('/' + dirname)
123+
const stats = await cozy.client.files.statByPath('/' + dirname)
124124
stats.should.have.property('attributes')
125125
stats.should.have.property('_id', created._id)
126126
})
127127

128128
it('trashes file or directory', async function () {
129129
const dirname = 'foo_' + random()
130130

131-
const created = await cozy.files.createDirectory({ name: dirname })
131+
const created = await cozy.client.files.createDirectory({ name: dirname })
132132
const createdId = created._id
133133

134-
await cozy.files.trashById(createdId)
134+
await cozy.client.files.trashById(createdId)
135135
})
136136

137137
it('downloads a file by path and id', async function () {
138138
const filename = 'foo_' + random()
139139

140-
const created = await cozy.files.create('foo', {
140+
const created = await cozy.client.files.create('foo', {
141141
name: filename,
142142
contentType: 'application/json'
143143
})
144144

145-
const downloaded1 = await cozy.files.downloadById(created._id)
145+
const downloaded1 = await cozy.client.files.downloadById(created._id)
146146
const txt1 = await downloaded1.text()
147147

148148
txt1.should.equal('foo')
149149

150-
const downloaded2 = await cozy.files.downloadByPath('/' + filename)
150+
const downloaded2 = await cozy.client.files.downloadByPath('/' + filename)
151151
const txt2 = await downloaded2.text()
152152

153153
txt2.should.equal('foo')
154154
})
155155

156156
it('destroy all trashed files and directories', async function () {
157-
await createTrashedDirectory(cozy, 'foo_' + random())
158-
await createTrashedDirectory(cozy, 'foo_' + random())
159-
await cozy.files.clearTrash()
157+
await createTrashedDirectory(cozy.client, 'foo_' + random())
158+
await createTrashedDirectory(cozy.client, 'foo_' + random())
159+
await cozy.client.files.clearTrash()
160160

161-
let trashed = await cozy.files.listTrash()
161+
let trashed = await cozy.client.files.listTrash()
162162
trashed.should.be.an.Array()
163163
trashed.should.have.length(0)
164164
})
165165

166166
it('list trashed files and directories', async function () {
167-
await cozy.files.clearTrash()
168-
const created1 = await createTrashedDirectory(cozy, 'foo_' + random())
169-
const created2 = await createTrashedDirectory(cozy, 'foo_' + random())
170-
let trashed = await cozy.files.listTrash()
167+
await cozy.client.files.clearTrash()
168+
const created1 = await createTrashedDirectory(cozy.client, 'foo_' + random())
169+
const created2 = await createTrashedDirectory(cozy.client, 'foo_' + random())
170+
let trashed = await cozy.client.files.listTrash()
171171
trashed.should.be.an.Array()
172172
trashed.should.have.length(2)
173173
let found1 = false
@@ -182,50 +182,50 @@ describe('files API', async function () {
182182
})
183183

184184
it('restore a trashed file or directory', async function () {
185-
await cozy.files.clearTrash()
186-
const created = await createTrashedDirectory(cozy, 'foo_' + random())
187-
await cozy.files.restoreById(created._id)
185+
await cozy.client.files.clearTrash()
186+
const created = await createTrashedDirectory(cozy.client, 'foo_' + random())
187+
await cozy.client.files.restoreById(created._id)
188188
})
189189

190190
it('destroy a trashed file or directory', async function () {
191-
await cozy.files.clearTrash()
192-
const created = await createTrashedDirectory(cozy, 'foo_' + random())
193-
await cozy.files.destroyById(created._id)
194-
let trashed = await cozy.files.listTrash()
191+
await cozy.client.files.clearTrash()
192+
const created = await createTrashedDirectory(cozy.client, 'foo_' + random())
193+
await cozy.client.files.destroyById(created._id)
194+
let trashed = await cozy.client.files.listTrash()
195195
trashed.should.be.an.Array()
196196
trashed.should.have.length(0)
197197
})
198198

199199
it('creates download link for 1 file', async function () {
200200
const filename = 'foo_' + random()
201-
const created = await cozy.files.create('foo', {
201+
const created = await cozy.client.files.create('foo', {
202202
name: filename,
203203
contentType: 'application/json'
204204
})
205205
const path = '/' + created.attributes.name
206-
let link = await cozy.files.getDowloadLink(path)
206+
let link = await cozy.client.files.getDowloadLink(path)
207207
let downloaded = await fetch(COZY_STACK_URL + link)
208208
const txt1 = await downloaded.text()
209209
txt1.should.equal('foo')
210210
})
211211

212212
it('creates download link for archive', async function () {
213213
const filename = 'foo_' + random()
214-
const created = await cozy.files.create('foo', {
214+
const created = await cozy.client.files.create('foo', {
215215
name: filename,
216216
contentType: 'application/json'
217217
})
218218

219219
const filename2 = 'bar_' + random()
220-
const created2 = await cozy.files.create('bar', {
220+
const created2 = await cozy.client.files.create('bar', {
221221
name: filename2,
222222
contentType: 'application/json'
223223
})
224224
const toDownload = [
225225
'/' + created.attributes.name,
226226
'/' + created2.attributes.name
227227
]
228-
let link = await cozy.files.getArchiveLink(toDownload, 'foobar')
228+
let link = await cozy.client.files.getArchiveLink(toDownload, 'foobar')
229229
let downloaded = await fetch(COZY_STACK_URL + link)
230230
downloaded.ok.should.be.true
231231
downloaded.headers.get('Content-Type').should.equal('application/zip')
@@ -235,21 +235,21 @@ describe('files API', async function () {
235235

236236
describe('offline', async () => {
237237
beforeEach(() => {
238-
cozy = new Cozy({
238+
cozy.client = new Client({
239239
cozyURL: COZY_STACK_URL,
240240
token: COZY_STACK_TOKEN,
241241
offline: {doctypes: ['io.cozy.files'], options: {adapter: 'memory'}}
242242
})
243243
})
244244
afterEach(() => {
245-
cozy.offline.destroyDatabase('io.cozy.files')
245+
cozy.client.offline.destroyDatabase('io.cozy.files')
246246
})
247247

248248
it('should be same document offline/online', async () => {
249-
const folder = await createRandomDirectory(cozy)
250-
await cozy.offline.replicateFromCozy('io.cozy.files')
251-
const offline = await cozy.files.statById(folder._id)
252-
const online = await cozy.files.statById(folder._id, false)
249+
const folder = await createRandomDirectory(cozy.client)
250+
await cozy.client.offline.replicateFromCozy('io.cozy.files')
251+
const offline = await cozy.client.files.statById(folder._id)
252+
const online = await cozy.client.files.statById(folder._id, false)
253253
Object.keys(online).forEach(key => { key === 'links' || offline.should.have.keys(key) })
254254
Object.keys(offline).forEach(key => { online.should.have.keys(key) })
255255
Object.keys(online.attributes).forEach(key => { offline.attributes.should.have.keys(key) })
@@ -258,14 +258,14 @@ describe('files API', async function () {
258258
})
259259
})
260260

261-
async function createTrashedDirectory (cozy, dirname) {
262-
const created = await cozy.files.createDirectory({ name: dirname })
263-
await cozy.files.trashById(created._id)
261+
async function createTrashedDirectory (client, dirname) {
262+
const created = await client.files.createDirectory({ name: dirname })
263+
await client.files.trashById(created._id)
264264
return created
265265
}
266266

267-
async function createRandomDirectory (cozy) {
267+
async function createRandomDirectory (client) {
268268
const dirname = 'foo_' + randomGenerator()()
269-
const created = await cozy.files.createDirectory({ name: dirname })
269+
const created = await client.files.createDirectory({ name: dirname })
270270
return created
271271
}

0 commit comments

Comments
 (0)