UNPKG

5.2 kBJavaScriptView Raw
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12
13/**
14 * The Fastly Purge API.
15 *
16 * @see https://docs.fastly.com/api/purge#purge
17 * @type {PurgeAPI}
18 */
19class PurgeAPI {
20 constructor(base) {
21 Object.assign(this, {
22 service_id: base.service_id,
23 request: base.request,
24 });
25 }
26
27 /**
28 * Instant Purge an individual URL.
29 *
30 * @see https://docs.fastly.com/api/purge#purge_3aa1d66ee81dbfed0b03deed0fa16a9a
31 * @param {string} url - The URL to purge.
32 * @returns {Promise} The response object representing the completion or failure.
33 * @example
34 * instance.purgeIndividual('www.example.com')
35 .then(res => {
36 console.log(res.data);
37 })
38 .catch(err => {
39 console.log(err.message);
40 });
41 */
42 async purgeIndividual(url) {
43 return this.request.post(`/purge/${url}`);
44 }
45
46 /**
47 * Instant Purge everything from a service.
48 *
49 * @see https://docs.fastly.com/api/purge#purge_bee5ed1a0cfd541e8b9f970a44718546
50 * @example
51 * instance.purgeAll()
52 .then(res => {
53 console.log(res.data);
54 })
55 .catch(err => {
56 console.log(err.message);
57 });
58 * @returns {Promise} The response object representing the completion or failure.
59 */
60 async purgeAll() {
61 return this.request.post(`/service/${this.service_id}/purge_all`);
62 }
63
64 /**
65 * Instant Purge a particular service of items tagged with a Surrogate Key.
66 *
67 * @see https://docs.fastly.com/api/purge#purge_d8b8e8be84c350dd92492453a3df3230
68 * @example
69 * instance.purgeKey('key_1')
70 .then(res => {
71 console.log(res.data);
72 })
73 .catch(err => {
74 console.log(err.message);
75 });
76 * @param {string} key - The surrogate key to purge.
77 * @returns {Promise} The response object representing the completion or failure.
78 */
79 async purgeKey(key) {
80 return this.request.post(`/service/${this.service_id}/purge/${key}`);
81 }
82
83 /**
84 * Instant Purge a particular service of items tagged with Surrogate Keys in a batch.
85 *
86 * @see https://docs.fastly.com/api/purge#purge_db35b293f8a724717fcf25628d713583
87 * @example
88 * instance.purgeKeys(['key_2', 'key_3', 'key_4'])
89 .then(res => {
90 console.log(res.data);
91 })
92 .catch(err => {
93 console.log(err.message);
94 });
95 * @param {Array} keys - The array of surrogate keys to purge.
96 * @returns {Promise} The response object representing the completion or failure.
97 */
98 async purgeKeys(keys) {
99 return this.request.post(`/service/${this.service_id}/purge`, {
100 surrogate_keys: keys,
101 }, {
102 headers: {
103 'content-type': 'application/json',
104 },
105 });
106 }
107
108 /**
109 * Soft Purge an individual URL.
110 *
111 * @param {string} url - The URL to soft purge.
112 * @see https://docs.fastly.com/api/purge#soft_purge_0c4f56f3d68e9bed44fb8b638b78ea36
113 * @example
114 * instance.softPurgeIndividual('www.example.com/images')
115 .then(res => {
116 console.log(res.data);
117 })
118 .catch(err => {
119 console.log(err.message);
120 });
121 * @returns {Promise} The response object representing the completion or failure.
122 */
123 async softPurgeIndividual(url) {
124 return this.request.post(`/purge/${url}`, undefined, { headers: { 'Fastly-Soft-Purge': 1 } });
125 }
126
127 /**
128 * Soft Purge a particular service of items tagged with a Surrogate Key.
129 *
130 * @see https://docs.fastly.com/api/purge#soft_purge_2e4d29085640127739f8467f27a5b549
131 * @example
132 * instance.softPurgeKey('key_5')
133 .then(res => {
134 console.log(res.data);
135 })
136 .catch(err => {
137 console.log(err.message);
138 });
139 * @param {string} key - The surrogate key to soft purge.
140 * @returns {Promise} The response object representing the completion or failure.
141 */
142 async softPurgeKey(key) {
143 return this.request.post(`/service/${this.service_id}/purge/${key}`, undefined, { headers: { 'Fastly-Soft-Purge': 1 } });
144 }
145
146 /**
147 * Soft Purge a particular service of items tagged with Surrogate Keys in a batch.
148 *
149 * @see https://docs.fastly.com/api/purge#purge_db35b293f8a724717fcf25628d713583
150 * @example
151 * instance.softPurgeKeys(['key_2', 'key_3', 'key_4'])
152 .then(res => {
153 console.log(res.data);
154 })
155 .catch(err => {
156 console.log(err.message);
157 });
158 * @param {Array} keys - The array of surrogate keys to purge.
159 * @returns {Promise} The response object representing the completion or failure.
160 */
161 async softPurgeKeys(keys) {
162 return this.request.post(`/service/${this.service_id}/purge`, {
163 surrogate_keys: keys,
164 }, {
165 headers: {
166 'Fastly-Soft-Purge': 1,
167 'content-type': 'application/json',
168 },
169 });
170 }
171}
172
173module.exports = PurgeAPI;