Edits history of script submission #16183 for ' Search in a site (gitbook)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Gitbook = {
      token: string;
    };
    /**
     * Search in a site
     *
     */
    export async function main(
      auth: Gitbook,
      organizationId: string,
      siteId: string,
      page: string | undefined,
      limit: string | undefined,
      body:
        | ({ query: string } & {
            scope:
              | { mode: "default"; includedSiteSpaces?: string[] }
              | { mode: "specific"; siteSpaceIds: string[] }
              | { mode: "all" };
          })
        | ({ query: string } & { mode?: "specific"; siteSpaceIds: string[] })
        | ({ query: string } & { mode: "current"; siteSpaceId: string })
        | ({ query: string } & { mode: "all" }),
    ) {
      const url = new URL(
        `https://api.gitbook.com/v1/orgs/${organizationId}/sites/${siteId}/search`,
      );
      for (const [k, v] of [
        ["page", page],
        ["limit", limit],
      ]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          Authorization: "Bearer " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago