prismicio / prismic-types

Type definitions for Prismic content, models, and APIs
https://prismic.io/docs/technologies/javascript
Apache License 2.0
11 stars 4 forks source link

Add webhook response types #3

Closed angeloashmore closed 2 years ago

angeloashmore commented 3 years ago

Is your feature request related to a problem? Please describe.

Consumers of Prismic's webhooks currently do not have an official way to type the incoming data. Developers will need to write the types in their own projects.

Describe the solution you'd like

@prismicio/types can provide these types to developers. As the API changes, or new webhooks are added, this library can be updated to ensure developers have the latest types.

Describe alternatives you've considered

Developers can write these types in their own project. They can also minimize the types to include only what is relevant to their application. This can lead to extra friction when adopting Prismic webhooks into an application as it requires additional work.

Additional context

The following types are taken from gatsby-source-prismic and could be used as a reference when adding to this library.

(Based on types from @MarcMcIntosh)

export type PrismicWebhookBody =
  | PrismicWebhookBodyApiUpdate
  | PrismicWebhookBodyTestTrigger

export enum PrismicWebhookType {
  APIUpdate = 'api-update',
  TestTrigger = 'test-trigger',
}

interface PrismicWebhookBodyBase {
  type: PrismicWebhookType
  domain: string
  apiUrl: string
  secret: string | null
}

export interface PrismicWebhookBodyApiUpdate extends PrismicWebhookBodyBase {
  type: PrismicWebhookType.APIUpdate
  masterRef?: string
  releases: PrismicWebhookOperations<PrismicWebhookRelease>
  masks: PrismicWebhookOperations<PrismicWebhookMask>
  tags: PrismicWebhookOperations<PrismicWebhookTag>
  documents: string[]
  // eslint-disable-next-line deprecation/deprecation
  experiments?: PrismicWebhookOperations<PrismicWebhookExperiment>
}

export interface PrismicWebhookBodyTestTrigger extends PrismicWebhookBodyBase {
  type: PrismicWebhookType.TestTrigger
}

interface PrismicWebhookOperations<T> {
  update?: T[]
  addition?: T[]
  deletion?: T[]
}

interface PrismicWebhookMask {
  id: string
  label: string
}

interface PrismicWebhookTag {
  id: string
}

export interface PrismicWebhookRelease {
  id: string
  ref: string
  label: string
  documents: string[]
}

/**
 * @deprecated
 */
interface PrismicWebhookExperiment {
  id: string
  name: string
  // eslint-disable-next-line deprecation/deprecation
  variations: PrismicWebhookExperimentVariation[]
}

/**
 * @deprecated
 */
interface PrismicWebhookExperimentVariation {
  id: string
  ref: string
  label: string
}
MarcMcIntosh commented 3 years ago

Nice :)

angeloashmore commented 2 years ago

Completed