From b2696a32007ab9dcdb6921ad32a9dfa8d95d8c11 Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Fri, 21 Aug 2020 15:29:31 -0400 Subject: [PATCH 1/2] Use a register hook and update to new plumber::register_docs() method --- R/paths.R | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/R/paths.R b/R/paths.R index e3af6b7..c8fd999 100644 --- a/R/paths.R +++ b/R/paths.R @@ -65,31 +65,36 @@ redoc_spec <- function(spec_url = "https://redocly.github.io/redoc/openapi.yaml" index_txt } -plumber_register_ui <- function() { - if (requireNamespace("plumber", quietly = TRUE)) { - register_ui <- tryCatch( - plumber::register_ui, - error = function(err) { - function(...) { - return() - } - } + + +plumber_docs <- function() { + list( + name = "redoc", + index = function(...) { + redoc::redoc_spec( + spec_url = "\' + window.location.origin + window.location.pathname.replace(/\\(__redoc__\\\\/|__redoc__\\\\/index.html\\)$/, '') + 'openapi.json' + \'", + ... ) - register_ui( - name = "redoc", - index = function(...) { - redoc::redoc_spec( - spec_url = "\' + window.location.origin + window.location.pathname.replace(/\\(__redoc__\\\\/|__redoc__\\\\/index.html\\)$/, '') + 'openapi.json' + \'", - ... - ) - }, - static = function(...) { - redoc::redoc_path() - } - ) - } + }, + static = function(...) { + redoc::redoc_path() + } + ) } .onLoad <- function(...) { - plumber_register_ui() + plumber_register_docs <- function() { + tryCatch({ + do.call(plumber::register_docs, plumber_docs()) + }, error = function(e) { + message("Error registering redoc docs. Error: ", e) + }) + } + + setHook(packageEvent("plumber", "onLoad"), function(...) { + plumber_register_docs() + }) + if ("plumber" %in% loadedNamespaces()) { + plumber_register_docs() + } } From 6ef2bda433c731b985aeb502b4e333ae316d946c Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Fri, 21 Aug 2020 15:36:48 -0400 Subject: [PATCH 2/2] use __docs__ location --- R/paths.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/paths.R b/R/paths.R index c8fd999..f705773 100644 --- a/R/paths.R +++ b/R/paths.R @@ -72,14 +72,14 @@ plumber_docs <- function() { name = "redoc", index = function(...) { redoc::redoc_spec( - spec_url = "\' + window.location.origin + window.location.pathname.replace(/\\(__redoc__\\\\/|__redoc__\\\\/index.html\\)$/, '') + 'openapi.json' + \'", + spec_url = "\' + window.location.origin + window.location.pathname.replace(/\\(__docs__\\\\/|__docs__\\\\/index.html\\)$/, '') + 'openapi.json' + \'", ... ) }, static = function(...) { redoc::redoc_path() } - ) + ) } .onLoad <- function(...) {