@prismicio/client 5.0.0-alpha.0 → 5.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,102 +1,591 @@
1
- # @prismicio/client
1
+ ## JavaScript development for prismic.io API v2
2
2
 
3
- [![npm version][npm-version-src]][npm-version-href]
4
- [![npm downloads][npm-downloads-src]][npm-downloads-href]
5
- [![Github Actions CI][github-actions-ci-src]][github-actions-ci-href]
6
- [![Codecov][codecov-src]][codecov-href]
7
- [![Conventional Commits][conventional-commits-src]][conventional-commits-href]
8
- [![License][license-src]][license-href]
3
+ [![npm version](https://badge.fury.io/js/prismic-javascript.svg)](http://badge.fury.io/js/prismic-javascript)
4
+ [![Build Status](https://api.travis-ci.org/prismicio/prismic-javascript.png)](https://travis-ci.org/prismicio/prismic-javascript)
9
5
 
10
- The official JavaScript + TypeScript client library for [Prismic][prismic].
6
+ * The [source code](https://github.com/prismicio/prismic-javascript) is on Github.
7
+ * The [Changelog](https://github.com/prismicio/prismic-javascript/releases) is on Github's releases tab.
8
+ * The [API reference](https://prismicio.github.io/prismic-javascript/globals.html) is on Github.
11
9
 
12
- - Query content from a Prismic repository.
13
- - Filter queries using [Predicates][prismic-predicates].
14
- - Automatically query draft content during [preview sessions][prismic-previews].
15
- - Built for browser and server usage.
10
+ It's meant to work in pair with the prismic-dom library available here:
11
+ * [prismic-dom](https://github.com/prismicio/prismic-dom) is on Github.
16
12
 
17
- ```typescript
18
- import * as prismic from "@prismicio/client";
13
+ 🎉Our library will hit v3 soon! That's right, it's the v3 of an API V2 package...
14
+ You can try it by installing the beta version from npm. [Read the changelog](https://github.com/prismicio/prismic-javascript/releases/tag/3.0.0)!
19
15
 
20
- // Create a client
21
- const endpoint = prismic.getEndpoint("my-repository");
22
- const client = prismic.createClient(endpoint);
16
+ ---
23
17
 
24
- // Then query for your content
25
- const blogPosts = await client.getAllByType("blog_post");
18
+
19
+ [1. Installation](#installation)
20
+ --------------------------------
21
+
22
+ > [Prismic API Settings](#prismic-api-settings) <br />
23
+ > [NPM](#npm) <br />
24
+ > [CDN](#cdn) <br />
25
+ > [Downloadable version](#downloadable-version) <br />
26
+ > [Polyfills](#polyfills) <br />
27
+ > [Demo project](#demo-project) <br />
28
+ > [Starter kits](#starter-kits) <br />
29
+
30
+ [2. Query the content](#query-the-content)
31
+ --------------------------------
32
+
33
+ [3. Integrate the content](#integrate-the-content)
34
+ -----------------------------------------------------
35
+
36
+ > [Embed](#embed) <br />
37
+ > [Image](#image) <br />
38
+ > [Text](#text) <br />
39
+ > [Number](#number) <br />
40
+ > [Date](#date) <br />
41
+ > [Timestamp](#timestamp) <br />
42
+ > [Select](#select) <br />
43
+ > [Color](#color) <br />
44
+ > [StructuredText](#structuredText) <br />
45
+ > [WebLink](#weblink) <br />
46
+ > [DocumentLink](#documentlink) <br />
47
+ > [ImageLink](#imagelink) <br />
48
+ > [FileLink](#filelink) <br />
49
+ > [Separator](#separator) <br />
50
+ > [Group](#group) <br />
51
+ > [GeoPoint](#geopoint) <br />
52
+ > [Slices](#slices) <br />
53
+
54
+ [4. Contribute to the kit](#contribute-to-the-kit)
55
+ --------------------------------
56
+ > [Install the kit locally](#install-the-kit-locally) <br />
57
+ > [Documentation](#documentation) <br />
58
+
59
+ [5. License](#license)
60
+ -----------------------------------------------------
61
+
62
+ ---
63
+
64
+ ### Installation
65
+
66
+ #### Prismic API Settings
67
+ Your endpoint must contains "v2" at the end, otherwise it means that you're working on the API V1 so this library won't work for you.
68
+
69
+ ```javascript
70
+ apiEndpoint: your-repo-name.prismic.io/api/v2
26
71
  ```
27
72
 
28
- ## Install
73
+ #### NPM
29
74
 
30
75
  ```sh
31
- npm install @prismicio/client
76
+ npm install @prismicio/client --save
77
+ ```
78
+
79
+ #### CDN
80
+
81
+ ```
82
+ https://unpkg.com/@prismicio/client
83
+ ```
84
+
85
+ (You may need to adapt the version number)
86
+
87
+ #### Downloadable version
88
+
89
+ On our release page: [https://github.com/prismicio/prismic-javascript/releases](https://github.com/prismicio/prismic-javascript/releases).
90
+
91
+ #### Polyfills
92
+ To support legacy browsers include a promise polyfill.
93
+
94
+ #### Starter kits
95
+
96
+ For new project, you can start from a sample project:
97
+
98
+ * [Node.js project](https://github.com/prismicio/nodejs-sdk)
99
+ * [Node.js blog](https://github.com/prismicio/nodejs-blog)
100
+
101
+ #### Demo project
102
+
103
+ You can find an integration of prismic content with the new API V2 in the following projects:
104
+ * [Next.js project](https://github.com/prismicio/nextjs-website)
105
+ * [Node.js project](https://github.com/prismicio/nodejs-website)
106
+
107
+ ### Instantiate the Prismic Client
108
+
109
+ The client is synchronous and allow you to make queries directly to your API.
110
+
111
+ ```javascript
112
+ const Prismic = require('@prismicio/client');
113
+
114
+ const options = {
115
+ // see specifications below
116
+ }
117
+ const client = Prismic.client("http://your_repository_name.cdn.prismic.io/api", options)
118
+ ```
119
+
120
+ Options:
121
+
122
+ | Param | Type | Required | Default Value | Description |
123
+ |---------------- |---------------------------- |---------- |--------------- |------------------------------------------------------------------------------------------------------------------------------ |
124
+ | accessToken | string | false | null | If your API is private, pass your token to access your content |
125
+ | routes | array | false | null | Options used by the API to resolve link urls (replaces linkResolver) |
126
+ | requestHandler | RequestHandler (see desc.) | false | null | See RequestHandler interface. Implements a request function and a callback to be called once request has returned. |
127
+ | req | object | false | null | Request object when using the client server-side (eg: Next.js, Express, Koa...) |
128
+ | apiCache | ApiCache (see desc.) | false | null | Swap default api cache with your own. Needs at least get and set properties. See ApiCache interface for full implementation. |
129
+ | apiDataTTL | number | false | 5 | Sets the refresh rate of API data. Used by the default api cache. |
130
+ | proxyAgent | any | false | null | Use your own proxy agent (eg. https://github.com/http-party/node-http-proxy) |
131
+ | timeoutInMs | number | false | null | Sets the timeout in milliseconds of requess. Used by the default request handler. |
132
+
133
+ ### Query the content
134
+
135
+ To fetch documents from your repository, you need to instantiate the client first.
136
+
137
+ ```javascript
138
+ const Prismic = require('@prismicio/client');
139
+
140
+ const client = Prismic.client("http://your_repository_name.cdn.prismic.io/api")
141
+ var options = {}; // In Node.js, pass the request as 'req' to read the reference from the cookies
142
+ client.query("", options, function(err, response) { // An empty query will return all the documents
143
+ if (err) {
144
+ console.log("Something went wrong: ", err);
145
+ }
146
+ console.log("Documents: ", response.documents);
147
+ });
148
+ ```
149
+
150
+ All asynchronous calls return ES2015 promises, so alternatively you can use them instead of callbacks.
151
+
152
+ ```javascript
153
+ const Prismic = require('@prismicio/client');
154
+
155
+ const client = Prismic.client("http://your_repository_name.cdn.prismic.io/api")
156
+ return client.query("") // An empty query will return all the documents
157
+ .then(function(response) {
158
+ console.log("Documents: ", response.results);
159
+ }, function(err) {
160
+ console.log("Something went wrong: ", err);
161
+ });
162
+ ```
163
+
164
+ If you included prismic through the script tag (CDN) there is a global variable PrismicJS:
165
+ ```javascript
166
+ const client = PrismicJS.client("http://your_repository_name.cdn.prismic.io/api")
167
+ const faq = PrismicJS.Predicates.at('document.type', 'faq');
168
+ client.query(faq, { lang: 'en-en' }).then( response => {
169
+ console.log("Documents: ", response.results);
170
+ })
32
171
  ```
33
172
 
34
- ## Documentation
173
+ See the [developer documentation](https://prismic.io/docs) or the [API documentation](https://prismicio.github.io/prismic-javascript/globals.html) for more details on how to use it.
35
174
 
36
- To discover what's new in this package, check out [the changelog][changelog].
37
- For full documentation, visit the [official Prismic
38
- documentation][prismic-docs].
175
+ ### Integrate the content
39
176
 
40
- ## Contributing
177
+ In each case documented below, you will have a snippet of the custom type and another for the code needed to fill the content field into your JS Template.
178
+ In these examples we have a `doc` parameter corresponding to the fetched prismic document.
41
179
 
42
- Whether you're helping us fix bugs, improve the docs, or spread the word, we'd
43
- love to have you as part of the Prismic developer community!
44
180
 
45
- **Asking a question**: [Open a new topic][forum-question] on our community forum
46
- explaining what you want to achieve / your question. Our support team will get
47
- back to you shortly.
181
+ #### Embed
182
+ Custom type
183
+ ```javascript
184
+ "video" : {
185
+ "type" : "Embed"
186
+ }
187
+ ```
48
188
 
49
- **Reporting a bug**: [Open an issue][repo-bug-report] explaining your
50
- application's setup and the bug you're encountering.
189
+ Template JS
190
+ ```javascript
191
+ doc.data.video.embed_url
192
+ ```
51
193
 
52
- **Suggesting an improvement**: [Open an issue][repo-feature-request] explaining
53
- your improvement or feature so we can discuss and learn more.
194
+ #### Image
195
+ Custom type
196
+ ```javascript
197
+ "photo" : {
198
+ "type" : "Image",
199
+ "fieldset" : "Image",
200
+ "config" : {
201
+ "constraint" : {
202
+ "width" : 300,
203
+ "height" : 300
204
+ },
205
+ "thumbnails" : [ {
206
+ "name" : "Small",
207
+ "width" : 100,
208
+ "height" : 100
209
+ }, {
210
+ "name" : "Medium",
211
+ "width" : 200,
212
+ "height" : 200
213
+ }, {
214
+ "name" : "Large",
215
+ "width" : 300,
216
+ "height" : 300
217
+ } ]
218
+ }
219
+ }
220
+ ```
221
+ Template JS
222
+ ```javascript
223
+ //main view
224
+ doc.data.photo.url
225
+ doc.data.photo.alt
226
+ doc.data.photo.width
227
+ doc.data.photo.height
54
228
 
55
- **Submitting code changes**: For small fixes, feel free to [open a
56
- PR][repo-pull-requests] with a description of your changes. For large changes,
57
- please first [open an issue][repo-feature-request] so we can discuss if and how
58
- the changes should be implemented.
229
+ //thumbnails => example for small view
230
+ doc.data.photo.small.url
231
+ doc.data.photo.small.alt
232
+ doc.data.photo.small.width
233
+ doc.data.photo.small.height
234
+ ```
235
+ #### Text
236
+ Custom type
237
+ ```javascript
238
+ "title" : {
239
+ "type" : "Text",
240
+ }
241
+ ```
59
242
 
60
- ## License
243
+ Template JS
244
+ ```javascript
245
+ doc.data.title
246
+ ```
247
+ #### Number
248
+ Custom type
249
+ ```javascript
250
+ "count" : {
251
+ "type" : "Text",
252
+ }
253
+ ```
61
254
 
255
+ Template JS
256
+ ```javascript
257
+ doc.data.count
258
+ ```
259
+ #### Date
260
+ Custom type
261
+ ```javascript
262
+ "publication" : {
263
+ "type" : "Date",
264
+ }
62
265
  ```
63
- Copyright 2013-2021 Prismic <contact@prismic.io> (https://prismic.io)
64
266
 
65
- Licensed under the Apache License, Version 2.0 (the "License"); you may not use
66
- this file except in compliance with the License. You may obtain a copy of the
67
- License at
267
+ Template JS
268
+ ```javascript
269
+ import { Date } from 'prismic-dom'
68
270
 
69
- http://www.apache.org/licenses/LICENSE-2.0
271
+ // date as string from the API
272
+ doc.data.publication
273
+ // date as JS Date
274
+ Date(doc.data.publication)
70
275
 
71
- Unless required by applicable law or agreed to in writing, software distributed
72
- under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
73
- CONDITIONS OF ANY KIND, either express or implied. See the License for the
74
- specific language governing permissions and limitations under the License.
75
276
  ```
277
+ #### Timestamp
278
+ Custom type
279
+ ```javascript
280
+ "time" : {
281
+ "type" : "Timestamp",
282
+ }
283
+ ```
284
+
285
+ Template JS
286
+ ```javascript
287
+ import { Date } from 'prismic-dom'
288
+
289
+ // timestamp as string from the API
290
+ doc.data.time
291
+ // timestamp as JS Datetime
292
+ Date(doc.data.time)
293
+ ```
294
+ #### Select
295
+ Custom type
296
+ ```javascript
297
+ "gender" : {
298
+ "type" : "Select",
299
+ }
300
+ ```
301
+
302
+ Template JS
303
+ ```javascript
304
+ doc.data.gender
305
+ ```
306
+ #### Color
307
+ Custom type
308
+ ```javascript
309
+ "background" : {
310
+ "type" : "Color",
311
+ }
312
+ ```
313
+
314
+ Template JS
315
+ ```javascript
316
+ doc.data.background
317
+ ```
318
+ #### RichText
319
+ Custom type
320
+ ```javascript
321
+ "description" : {
322
+ "type" : "StructuredText",
323
+ }
324
+ ```
325
+
326
+ Template JS
327
+ ```javascript
328
+ import { RichText } from 'prismic-dom'
329
+
330
+ RichText.asText(doc.data.description)
331
+
332
+ //linkResolver must be declare somewhere
333
+ RichText.asHtml(doc.data.description, linkResolver)
334
+ ```
335
+
336
+ #### WebLink
337
+ Custom type
338
+ ```javascript
339
+ "linktoweb" : {
340
+ "type" : "Link",
341
+ "config" : {
342
+ "select" : "web"
343
+ }
344
+ }
345
+ ```
346
+
347
+ Template JS
348
+ ```javascript
349
+ doc.data.linktoweb.url
350
+ ```
351
+ #### DocumentLink
352
+ Custom type
353
+ ```javascript
354
+ "linktodoc" : {
355
+ "type" : "Link",
356
+ "config" : {
357
+ "select" : "document",
358
+ "customtypes" : [ <your-custom-type-id> ],
359
+ "tags" : [ <your-tag> ],
360
+ }
361
+ }
362
+ ```
363
+
364
+ Template JS
365
+ ```javascript
366
+ //return url of the document link
367
+ doc.data.linktodoc
368
+ //return url of the document
369
+ linkResolver(doc.data.linktodoc)
370
+ ```
371
+ #### ImageLink
372
+ Custom type
373
+ ```javascript
374
+ "linktomedia" : {
375
+ "type" : "Link",
376
+ "config" : {
377
+ "select" : "media"
378
+ }
379
+ }
380
+ ```
381
+
382
+ Template JS
383
+ ```javascript
384
+ doc.data.linktomedia.url
385
+ ```
386
+ #### FileLink
387
+ Custom type
388
+ ```javascript
389
+ "linktofile" : {
390
+ "type" : "Link",
391
+ "config" : {
392
+ "select" : "media"
393
+ }
394
+ }
395
+ ```
396
+
397
+ Template JS
398
+ ```javascript
399
+ doc.data.linktofile.url
400
+ ```
401
+ #### Group
402
+ Custom type
403
+ ```javascript
404
+ "feature" : {
405
+ "type" : "Group",
406
+ "repeat": true, //default to true but put explicitly for the example
407
+ "config" : {
408
+ "field" : {
409
+ "title" : {
410
+ "type" : "Text",
411
+ },
412
+ "description" : {
413
+ "type" : "StructuredText",
414
+ }
415
+ }
416
+ }
417
+ }
418
+ ```
419
+
420
+ Template JS
421
+ ```javascript
422
+ import { RichText } from 'prismic-dom'
423
+
424
+ doc.data.feature.forEach(item => {
425
+ item.title
426
+ RichText.asHtml(item.description, linkResolver)
427
+ })
428
+ ```
429
+ #### GeoPoint
430
+ Custom type
431
+ ```javascript
432
+ "location" : {
433
+ "type" : "GeoPoint",
434
+ }
435
+ ```
436
+
437
+ Template JS
438
+ ```javascript
439
+ doc.data.latitude
440
+ doc.data.longitude
441
+ ```
442
+ #### Slices
443
+ **Slice with Group as value**
444
+ The Group value will be put directly as Slice value
445
+ Custom type
446
+ ```javascript
447
+ "contentAsSlices" : {
448
+ "fieldset" : "Dynamic page zone...",
449
+ "type" : "Slices",
450
+ "config" : {
451
+ "choices" : {
452
+ "slides" : {
453
+ "type" : "Group",
454
+ //required to display name in slice select in the writing room
455
+ "fieldset" : "Slides",
456
+ "config" : {
457
+ "fields" : {
458
+ "illustration" : {
459
+ "type" : "Image"
460
+ },
461
+ "title" : {
462
+ "type" : "StructuredText"
463
+ }
464
+ }
465
+ }
466
+ }
467
+ }
468
+ }
469
+ }
470
+ ```
471
+
472
+ Template JS
473
+ ```javascript
474
+ for(slice in doc.data.contentAsSlices) {
475
+ switch(slice.slice_type) {
476
+ case 'slides':
477
+ slice.value.forEach(item => {
478
+ item.illustration.url
479
+ item.title
480
+ })
481
+ break
482
+ }
483
+ }
484
+
485
+ ```
486
+ **Slice with basic fragment like Text as value**
487
+ The fragment value will be put directly as Slice value
488
+ Custom type
489
+ ```javascript
490
+ "contentAsSlices" : {
491
+ "fieldset" : "Dynamic page zone...",
492
+ "type" : "Slices",
493
+ "config" : {
494
+ "choices" : {
495
+ "description" : {
496
+ "type" : "StructuredText"
497
+ }
498
+ }
499
+ }
500
+ }
501
+ ```
502
+
503
+ Template JS
504
+ ```javascript
505
+ import { RichText } from 'prismic-dom'
506
+
507
+ for(slice in doc.contentAsSlices) {
508
+ switch(slice.slice_type) {
509
+ case 'description':
510
+ RichText.asHtml(slice.value, linkResolver)
511
+ break
512
+ }
513
+ }
514
+
515
+ ```
516
+
517
+ **new Slice**
518
+ the new Slice type allow you to create a repeatable area and a non repeatable one.
519
+ ```javascript
520
+ "contentAsSlices" : {
521
+ "fieldset" : "Dynamic page zone...",
522
+ "type" : "Slices",
523
+ "config" : {
524
+ "choices" : {
525
+ "newslice" : {
526
+ "type" : "Slice",
527
+ "non-repeat": {
528
+ "title": {
529
+ "type": "Text"
530
+ }
531
+ },
532
+ "repeat": {
533
+ "description": {
534
+ "type" : "StructuredText"
535
+ }
536
+ }
537
+ }
538
+ }
539
+ }
540
+ }
541
+ ```
542
+
543
+ Template JS
544
+ ```javascript
545
+ import { RichText } from 'prismic-dom'
546
+
547
+ for(slice in doc.contentAsSlices) {
548
+ switch(slice.slice_type) {
549
+ case 'newslice':
550
+ //non repeatable part
551
+ slice.value.primary.title
552
+
553
+ //repeatable part
554
+ slice.value.items.forEach(item => {
555
+ RichText.asHtml(item.description, linkResolver)
556
+ })
557
+ break
558
+ }
559
+ }
560
+
561
+ ```
562
+
563
+ ### Contribute to the kit
564
+
565
+ #### Install the kit locally
566
+
567
+ Source files are in the `src/` directory. You only need [Node.js and npm](http://www.joyent.com/blog/installing-node-and-npm/)
568
+ to work on the codebase.
569
+
570
+ ```bash
571
+ npm install
572
+ npm run dev
573
+ ```
574
+
575
+ #### Documentation
576
+
577
+ Please document any new feature or bugfix using the [JSDoc](http://usejsdoc.org/) syntax. You don't need to generate the documentation, we'll do that.
578
+
579
+ If you feel an existing area of code is lacking documentation, feel free to write it; but please do so on its own branch and pull-request.
580
+
581
+ If you find existing code that is not optimally documented and wish to make it better, we really appreciate it; but you should document it on its own branch and its own pull request.
582
+
583
+ ### License
76
584
 
77
- <!-- Links -->
585
+ This software is licensed under the Apache 2 license, quoted below.
78
586
 
79
- [prismic]: https://prismic.io
80
- [prismic-predicates]: https://prismic.io/docs/technologies/query-predicate-reference-javascript
81
- [prismic-previews]: https://prismic.io/docs/core-concepts/previews
82
- [prismic-docs]: https://prismic.io/docs/technologies/javascript
83
- [changelog]: /CHANGELOG.md
84
- [forum-question]: https://community.prismic.io/c/kits-and-dev-languages/javascript/14
85
- [repo-bug-report]: https://github.com/prismicio/prismic-javascript/issues/new?assignees=&labels=bug&template=bug_report.md&title=
86
- [repo-feature-request]: https://github.com/prismicio/prismic-javascript/issues/new?assignees=&labels=enhancement&template=feature_request.md&title=
87
- [repo-pull-requests]: https://github.com/prismicio/prismic-javascript/pulls
587
+ Copyright 2013-2017 Prismic.io (http://prismic.io).
88
588
 
89
- <!-- Badges -->
589
+ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
90
590
 
91
- [npm-version-src]: https://img.shields.io/npm/v/@prismicio/client/latest.svg
92
- [npm-version-href]: https://npmjs.com/package/@prismicio/client
93
- [npm-downloads-src]: https://img.shields.io/npm/dm/@prismicio/client.svg
94
- [npm-downloads-href]: https://npmjs.com/package/@prismicio/client
95
- [github-actions-ci-src]: https://github.com/prismicio/prismic-javascript/workflows/ci/badge.svg
96
- [github-actions-ci-href]: https://github.com/prismicio/prismic-javascript/actions?query=workflow%3Aci
97
- [codecov-src]: https://img.shields.io/codecov/c/github/prismicio/prismic-javascript.svg
98
- [codecov-href]: https://codecov.io/gh/prismicio/prismic-javascript
99
- [conventional-commits-src]: https://img.shields.io/badge/Conventional%20Commits-1.0.0-yellow.svg
100
- [conventional-commits-href]: https://conventionalcommits.org
101
- [license-src]: https://img.shields.io/npm/l/@prismicio/client.svg
102
- [license-href]: https://npmjs.com/package/@prismicio/client
591
+ Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.