-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathstack.ts
More file actions
188 lines (172 loc) · 6.85 KB
/
stack.ts
File metadata and controls
188 lines (172 loc) · 6.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { StackConfig, SyncStack, SyncType, LivePreviewQuery } from './types';
import { AxiosInstance } from '@contentstack/core';
import { Asset } from './asset';
import { AssetQuery } from './asset-query';
import { ContentType } from './content-type';
import { ContentTypeQuery } from './contenttype-query';
import { synchronization } from './synchronization';
import {TaxonomyQuery} from './taxonomy-query';
import { GlobalFieldQuery } from './global-field-query';
import { GlobalField } from './global-field';
export class Stack {
readonly config: StackConfig;
private _client: AxiosInstance;
constructor(client: AxiosInstance, config: StackConfig) {
this._client = client;
this.config = config;
this._client.stackConfig = this.config;
}
/**
* @method asset
* @memberOf Stack
* @param {String} uid - uid of the asset
* @description Creates an object for all assets of a stack by default. To retrieve a single asset, specify its UID.
*
* @returns {Asset}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const asset = stack.asset() // For collection of asset
* // OR
* const asset = stack.asset('assetUid') // For a single asset with uid 'assetUid'
*
*/
asset(uid: string): Asset;
asset(): AssetQuery;
asset(uid?: string): Asset | AssetQuery {
if (uid) return new Asset(this._client, uid);
return new AssetQuery(this._client);
}
/**
* @method contentType
* @memberOf Stack
* @param {String} uid - uid of the asset
* @description Retrieves all contentTypes of a stack by default. To retrieve a single asset, specify its UID.
*
* @returns {ContentType}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const contentType = stack.contentType() // For collection of contentType
* // OR
* const contentType = stack.contentType('contentTypeUid') // For a single contentType with uid 'contentTypeUid'
*/
contentType(): ContentTypeQuery;
contentType(uid: string): ContentType;
contentType(uid?: string): ContentType | ContentTypeQuery {
if (uid) return new ContentType(this._client, uid);
return new ContentTypeQuery(this._client);
}
/**
* @method Taxonomy
* @memberOf Stack
* @description Sets the url to /taxonomies/entries. Pass a query to fetch entries with taxonomies
*
* @returns {TaxonomyQuery} * @example
* import contentstack from '@contentstack/typescript'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const taxonomy = stack.taxonomy() // For taxonomy query object
*/
taxonomy(): TaxonomyQuery {
return new TaxonomyQuery(this._client)
};
/**
* @method GlobalField
* @memberOf Stack
* @param {String} uid - uid of the asset
* @description Retrieves all contentTypes of a stack by default. To retrieve a single asset, specify its UID.
*
* @returns {ContentType}
* const contentType = stack.contentType() // For collection of contentType
* // OR
* const contentType = stack.contentType('contentTypeUid') // For a single contentType with uid 'contentTypeUid'
*/
globalField(): GlobalFieldQuery;
globalField(uid: string): GlobalField;
globalField(uid?: string): GlobalField | GlobalFieldQuery {
if (uid) return new GlobalField(this._client, uid);
return new GlobalFieldQuery(this._client);
}
/**
* @method setLocale
* @memberOf Stack
* @description Sets the locale of the API server
* @param {String} locale - valid locale e.g. fr-fr
* @return {Stack}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* stack.setLocale('en-155');
*/
setLocale(locale: string) {
this.config.locale = locale;
this._client.defaults.params.locale = locale;
}
/**
* @method sync
* @memberOf Stack
* @description Syncs your Contentstack data with your app and ensures that the data is always up-to-date by providing delta updates
* @param {object} params - params is an object that supports ‘locale’, ‘start_date’, ‘content_type_uid’, and ‘type’ queries.
* @example
* Stack.sync() // For initializing sync
* @example
* Stack.sync({ 'locale': 'en-us'}) //For initializing sync with entries of a specific locale
* @example
* Stack.sync({ 'start_date': '2018-10-22'}) //For initializing sync with entries published after a specific date
* @example
* Stack.sync({ 'content_type_uid': 'session'}) //For initializing sync with entries of a specific content type
* @example
* Stack.sync({ 'type': 'entry_published'})
* //Use the type parameter to get a specific type of content. Supports'asset_published',
* // 'entry_published', 'asset_unpublished', 'entry_unpublished', 'asset_deleted', 'entry_deleted', 'content_type_deleted'.
* @example
* Stack.sync({'pagination_token': '<page_tkn>'}) // For fetching the next batch of entries using pagination token
* @example
* Stack.sync({'sync_token': '<sync_tkn>'}) // For performing subsequent sync after initial sync
* @instance
*/
async sync(params: SyncType | SyncStack = {}, recursive = false) {
return await synchronization(this._client, params, recursive);
}
livePreviewQuery(query: LivePreviewQuery) {
if (this.config.live_preview) {
let livePreviewParams: any = { ...this.config.live_preview };
if (query.live_preview) {
livePreviewParams = {
...livePreviewParams,
live_preview: query.live_preview,
contentTypeUid: query.contentTypeUid || query.content_type_uid,
entryUid: query.entryUid || query.entry_uid,
preview_timestamp: query.preview_timestamp || "",
include_applied_variants: query.include_applied_variants || false,
};
} else {
livePreviewParams = {
live_preview: null,
contentTypeUid: null,
entryUid: null,
preview_timestamp: null,
include_applied_variants: false,
};
}
this._client.stackConfig.live_preview = livePreviewParams;
}
if (query.hasOwnProperty('release_id')) {
this._client.defaults.headers['release_id'] = query.release_id;
} else {
delete this._client.defaults.headers['release_id'];
}
if (query.hasOwnProperty('preview_timestamp')) {
this._client.defaults.headers['preview_timestamp'] = query.preview_timestamp;
} else {
delete this._client.defaults.headers['preview_timestamp'];
}
}
getClient(): any {
return this._client;
}
}