0

Post terminal readers reader

Published Oct 30, 2023

Updates a Reader object by setting the values of the parameters passed. Any parameters not provided will be left unchanged.

Script stripe
  • Submitted by hugo697 Typescript (fetch-only)
    Created 557 days ago
    1
    type Stripe = {
    2
      token: string;
    3
    };
    4
    /**
    5
     * Post terminal readers reader
    6
     * Updates a Reader object by setting the values of the parameters passed. Any parameters not provided will be left unchanged.
    7
     */
    8
    export async function main(
    9
      auth: Stripe,
    10
      reader: string,
    11
      body: {
    12
        expand?: string[];
    13
        label?: string | "";
    14
        metadata?: { [k: string]: string } | "";
    15
      }
    16
    ) {
    17
      const url = new URL(`https://api.stripe.com/v1/terminal/readers/${reader}`);
    18
    
    
    19
      const response = await fetch(url, {
    20
        method: "POST",
    21
        headers: {
    22
          "Content-Type": "application/x-www-form-urlencoded",
    23
          Authorization: "Bearer " + auth.token,
    24
        },
    25
        body: encodeParams(body),
    26
      });
    27
      if (!response.ok) {
    28
        const text = await response.text();
    29
        throw new Error(`${response.status} ${text}`);
    30
      }
    31
      return await response.json();
    32
    }
    33
    
    
    34
    function encodeParams(o: any) {
    35
      function iter(o: any, path: string) {
    36
        if (Array.isArray(o)) {
    37
          o.forEach(function (a) {
    38
            iter(a, path + "[]");
    39
          });
    40
          return;
    41
        }
    42
        if (o !== null && typeof o === "object") {
    43
          Object.keys(o).forEach(function (k) {
    44
            iter(o[k], path + "[" + k + "]");
    45
          });
    46
          return;
    47
        }
    48
        data.push(path + "=" + o);
    49
      }
    50
      const data: string[] = [];
    51
      Object.keys(o).forEach(function (k) {
    52
        if (o[k] !== undefined) {
    53
          iter(o[k], k);
    54
        }
    55
      });
    56
      return new URLSearchParams(data.join("&"));
    57
    }
    58