~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
48 lines · 1.7 KB Raw History
// Creating a repository, public or private.
//
// The repository record is the owner's own, so creating runs as the owner,
// on the owner's own PDS, under the owner's OAuth session. The server
// accepts it only from a token whose account is the site's, so nobody
// else's session creates here.

import { XrpcError } from '@pdsjs/git/client';
import { account } from '#/lib/git.js';
import { authedFetch } from '#/lib/oauth.js';

/**
 * Create an empty repository. The record exists at once, with no refs and
 * no bundles, and the first push fills it. A private one is kept to the
 * people the owner adds; the server makes the space that holds it, keyed by
 * the repository's own name, so the address stays the same as a public
 * repository's.
 * @param {import('#/lib/oauth.js').Session} session - the owner's own
 * @param {Object} about
 * @param {string} about.name
 * @param {string} [about.description]
 * @param {boolean} [about.private] - kept to the people the owner adds
 * @returns {Promise<{name: string, space: string|null}>}
 */
export async function createRepo(session, about) {
  const response = await authedFetch(
    session,
    `${account.pds}/xrpc/dev.pdsjs.git.createRepo`,
    {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({
        name: about.name,
        ...(about.description ? { description: about.description } : {}),
        ...(about.private ? { private: true } : {}),
      }),
    },
  );
  const body = await response.json().catch(() => ({}));
  if (!response.ok) {
    throw new XrpcError(
      response.status,
      body.error ?? 'Unknown',
      body.message ?? `The create failed (${response.status}).`,
    );
  }
  return body;
}