Edits history of script submission #14396 for ' Get current user (box)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Box = {
      token: string;
    };
    /**
     * Get current user
     * Retrieves information about the user who is currently authenticated.
    
    In the case of a client-side authenticated OAuth 2.0 application
    this will be the user who authorized the app.
    
    In the case of a JWT, server-side authenticated application
    this will be the service account that belongs to the application
    by default.
    
    Use the `As-User` header to change who this API call is made on behalf of.
     */
    export async function main(auth: Box, fields: string | undefined) {
      const url = new URL(`https://api.box.com/2.0/users/me`);
      for (const [k, v] of [["fields", fields]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "GET",
        headers: {
          Authorization: "Bearer " + auth.token,
        },
        body: undefined,
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago