From 721a8c2e66c4e98c303826bd6ab344de5c1e3ba8 Mon Sep 17 00:00:00 2001 From: Bastien Wirtz Date: Mon, 18 Nov 2024 14:01:37 +0100 Subject: [PATCH] feat(ping): support for custom http success codes. Fix #425 --- docs/customservices.md | 3 ++- src/mixins/service.js | 7 ++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/docs/customservices.md b/docs/customservices.md index a914d00..733c151 100644 --- a/docs/customservices.md +++ b/docs/customservices.md @@ -145,7 +145,7 @@ API key can be generated in Settings > Administration > Auth Tokens ## Ping -This card checks if the target link is available. All you need is to set the `type` to `Ping` and provide a url. By default the HEAD method is used but it can be configured to use GET using the optional `method` property. By default, the subtitle line shows the round trip time (RTT) of the request, unless you provide the `subtitle` property. +This card checks if the target link is available. All you need is to set the `type` to `Ping` and provide a url. By default the HEAD method is used but it can be configured to use GET using the optional `method` property. By default, the subtitle line shows the round trip time (RTT) of the request, unless you provide the `subtitle` property. Optionnaly, use `successCodes` to define which HTTP response status codes should be considered as available status. ```yaml - name: "Awesome app" @@ -153,6 +153,7 @@ This card checks if the target link is available. All you need is to set the `ty logo: "assets/tools/sample.png" url: "https://www.wikipedia.org/" # method: "head" + # successCodes: [200, 418] # optional, default to all 2xx HTTP response status codes # timeout: 500 # in ms. default 2000 # subtitle: "Bookmark example" # By default, request round trip time is displayed when subtitle is not set. ``` diff --git a/src/mixins/service.js b/src/mixins/service.js index 5ace491..b4fd2ad 100644 --- a/src/mixins/service.js +++ b/src/mixins/service.js @@ -47,7 +47,12 @@ export default { } return fetch(url, options).then((response) => { - if (!response.ok) { + let success = response.ok; + if (Array.isArray(this.item.successCodes)) { + success = this.item.successCodes.includes(response.status); + } + + if (!success) { throw new Error(`Ping: target not available (${response.status} error)`); }