From 386afdc794eefd5bcbc47a3fd7b898a07f69f978 Mon Sep 17 00:00:00 2001 From: sterni Date: Mon, 5 Apr 2021 01:58:26 +0200 Subject: feat(web/bubblegum): allow passing status as an int The whole pass the name of the status as a string thing was mostly born out of an overeager use of yants. It is still very neat especially for common cases like "OK", so we'll keep it, but also allow passing the integer variant of the status as well which probably feels more natural for a lot of people, especially over getting the casing right for "I'm a teapot". Change-Id: I3f012a291447ef385efdd28132292a8b331998c0 Reviewed-on: https://cl.tvl.fyi/c/depot/+/2850 Tested-by: BuildkiteCI Reviewed-by: sterni --- web/bubblegum/default.nix | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'web/bubblegum/default.nix') diff --git a/web/bubblegum/default.nix b/web/bubblegum/default.nix index 393ac75d48..aecc701886 100644 --- a/web/bubblegum/default.nix +++ b/web/bubblegum/default.nix @@ -89,12 +89,12 @@ let See the [README](./README.md) for an example. - Type: string -> attrs string -> string -> string + Type: either int string -> attrs string -> string -> string */ respond = - # response status as the textual representation in the - # HTTP protocol. See `statusCodes` for a list of valid - # options. + # response status as an integer (status code) or its + # textual representation in the HTTP protocol. + # See `statusCodes` for a list of valid options. statusArg: # headers as an attribute set of strings headers: @@ -102,7 +102,14 @@ let bodyArg: let status = - if builtins.isString statusArg then { + if builtins.isInt statusArg + then { + code = statusArg; + line = lib.findFirst + (line: statusCodes."${line}" == statusArg) + null + (builtins.attrNames statusCodes); + } else if builtins.isString statusArg then { code = statusCodes."${statusArg}" or null; line = statusArg; } else { -- cgit 1.4.1