Skip to content

Commit 4eb8671

Browse files
authored
Apply patch + rename usecache to useCache
Applying changes from #17298 (comment) + rename usecache to useCache to make all variables/constants follow lowerCamelCase.
1 parent 06459ce commit 4eb8671

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/content/docs/1.1.1.1/other-ways-to-use-1.1.1.1/dns-in-google-sheets.mdx

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Details } from "~/components"
1212
1.1.1.1 works directly inside Google Sheets. To get started, create a [Google Function](https://developers.google.com/apps-script/guides/sheets/functions) with the following code:
1313

1414
```js
15-
function NSLookup(type, domain, usecache = false, cachettl = 30) {
15+
function NSLookup(type, domain, useCache = false, minCacheTTL = 30) {
1616

1717
if (typeof type == 'undefined') {
1818
throw new Error('Missing parameter 1 dns type');
@@ -22,47 +22,48 @@ function NSLookup(type, domain, usecache = false, cachettl = 30) {
2222
throw new Error('Missing parameter 2 domain name');
2323
}
2424

25-
if (typeof usecache != "boolean") {
25+
if (typeof useCache != "boolean") {
2626
throw new Error('Only boolean values allowed in 3 use cache');
2727
}
2828

29-
if (typeof cachettl != "number") {
30-
throw new Error('Only numeric values allowed in 4 cache ttl');
29+
if (typeof minCacheTTL != "number") {
30+
throw new Error('Only numeric values allowed in 4 min cache ttl');
3131
}
3232

3333
type = type.toUpperCase();
3434
domain = domain.toLowerCase();
3535

36-
if (usecache) {
36+
let cache = null;
37+
if (useCache) {
3738
// Cache key and hash
3839
cacheKey = domain + "@" + type;
3940
cacheHash = Utilities.base64Encode(cacheKey);
4041
cacheBinKey = "nslookup-result-" + cacheHash;
4142

42-
var cache = CacheService.getScriptCache();
43-
var cachedResult = cache.get(cacheBinKey);
43+
cache = CacheService.getScriptCache();
44+
const cachedResult = cache.get(cacheBinKey);
4445
if (cachedResult != null) {
4546
return cachedResult;
4647
}
4748
}
4849

49-
var url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type);
50-
var options = {
50+
const url = 'https://cloudflare-dns.com/dns-query?name=' + encodeURIComponent(domain) + '&type=' + encodeURIComponent(type);
51+
const options = {
5152
muteHttpExceptions: true,
5253
headers: {
5354
accept: "application/dns-json"
5455
}
5556
};
5657

57-
var result = UrlFetchApp.fetch(url, options);
58-
var rc = result.getResponseCode();
59-
var resultText = result.getContentText();
58+
const result = UrlFetchApp.fetch(url, options);
59+
const rc = result.getResponseCode();
60+
const resultText = result.getContentText();
6061

6162
if (rc !== 200) {
6263
throw new Error(rc);
6364
}
6465

65-
var errors = [
66+
const errors = [
6667
{ name: "NoError", description: "No Error"}, // 0
6768
{ name: "FormErr", description: "Format Error"}, // 1
6869
{ name: "ServFail", description: "Server Failure"}, // 2
@@ -75,24 +76,25 @@ function NSLookup(type, domain, usecache = false, cachettl = 30) {
7576
{ name: "NotAuth", description: "Not Authorized"} // 9
7677
];
7778

78-
var response = JSON.parse(resultText);
79+
const response = JSON.parse(resultText);
7980

8081
if (response.Status !== 0) {
8182
return errors[response.Status].name;
8283
}
8384

84-
var outputData = [];
85-
var minCacheTTL = cachettl;
85+
const outputData = [];
86+
let cacheTTL = 0;
8687

8788
for (var i in response.Answer) {
8889
outputData.push(response.Answer[i].data);
89-
minCacheTTL = Math.max(minCacheTTL, response.Answer[i].TTL);
90+
const ttl = response.Answer[i].TTL;
91+
cacheTTL = Math.min(cacheTTL || ttl, ttl);
9092
}
9193

92-
var outputString = outputData.join(',');
94+
const outputString = outputData.join(',');
9395

94-
if (usecache) {
95-
cache.put(cacheBinKey, outputString, minCacheTTL);
96+
if (useCache) {
97+
cache.put(cacheBinKey, outputString, Math.max(cacheTTL, minCacheTTL));
9698
}
9799

98100
return outputString;

0 commit comments

Comments
 (0)