Convert more files to TypeScript sync.js & find-page.js (#51775)
This commit is contained in:
@@ -3,14 +3,26 @@ import { existsSync } from 'fs'
|
||||
import path from 'path'
|
||||
import { mkdirp } from 'mkdirp'
|
||||
|
||||
import { WEBHOOK_DATA_DIR, WEBHOOK_SCHEMA_FILENAME } from '../lib/index.js'
|
||||
import Webhook from './webhook.js'
|
||||
import { WEBHOOK_DATA_DIR, WEBHOOK_SCHEMA_FILENAME } from '../lib/index'
|
||||
import Webhook, { WebhookSchema } from '@/webhooks/scripts/webhook'
|
||||
|
||||
export async function syncWebhookData(sourceDirectory, webhookSchemas) {
|
||||
interface WebhookFile {
|
||||
webhooks?: {
|
||||
post: WebhookSchema
|
||||
}[]
|
||||
'x-webhooks'?: {
|
||||
post: WebhookSchema
|
||||
}[]
|
||||
}
|
||||
|
||||
export async function syncWebhookData(
|
||||
sourceDirectory: string,
|
||||
webhookSchemas: string[],
|
||||
): Promise<void> {
|
||||
await Promise.all(
|
||||
webhookSchemas.map(async (schemaName) => {
|
||||
const file = path.join(sourceDirectory, schemaName)
|
||||
const schema = JSON.parse(await readFile(file, 'utf-8'))
|
||||
const schema: WebhookFile = JSON.parse(await readFile(file, 'utf-8'))
|
||||
// In OpenAPI version 3.1, the schema data is under the `webhooks`
|
||||
// key, but in 3.0 the schema data was in `x-webhooks`.
|
||||
// We just fallback to `x-webhooks` for now since there's
|
||||
@@ -47,7 +59,7 @@ export async function syncWebhookData(sourceDirectory, webhookSchemas) {
|
||||
)
|
||||
}
|
||||
|
||||
async function processWebhookSchema(webhooks) {
|
||||
async function processWebhookSchema(webhooks: Webhook[]): Promise<void> {
|
||||
try {
|
||||
if (webhooks.length) {
|
||||
await Promise.all(webhooks.map((webhook) => webhook.process()))
|
||||
@@ -62,9 +74,11 @@ async function processWebhookSchema(webhooks) {
|
||||
// Create an object with all webhooks where the key is the webhook name.
|
||||
// Webhooks typically have a property called `action` that describes the
|
||||
// events that trigger the webhook. Some webhooks (like `ping`) don't have
|
||||
// action types -- in that case we set a the value of action to 'default'.
|
||||
async function formatWebhookData(webhooks) {
|
||||
const categorizedWebhooks = {}
|
||||
// action types -- in that case we set the value of action to 'default'.
|
||||
async function formatWebhookData(
|
||||
webhooks: Webhook[],
|
||||
): Promise<Record<string, Record<string, Webhook>>> {
|
||||
const categorizedWebhooks: Record<string, Record<string, Webhook>> = {}
|
||||
for (const webhook of Object.values(webhooks)) {
|
||||
if (!webhook.action) webhook.action = 'default'
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
#!/usr/bin/env node
|
||||
import { get, isPlainObject } from 'lodash-es'
|
||||
|
||||
import { getJsonValidator } from '#src/tests/lib/validate-json-schema.js'
|
||||
import { renderContent } from '#src/content-render/index.js'
|
||||
import webhookSchema from './webhook-schema.js'
|
||||
import { getBodyParams } from '../../rest/scripts/utils/get-body-params.js'
|
||||
import { getJsonValidator } from '@/tests/lib/validate-json-schema'
|
||||
import { renderContent } from '@/content-render/index'
|
||||
import webhookSchema from './webhook-schema'
|
||||
import { getBodyParams, TransformedParam } from '../../rest/scripts/utils/get-body-params'
|
||||
|
||||
const NO_CHILD_PROPERTIES = [
|
||||
'action',
|
||||
@@ -17,13 +16,45 @@ const NO_CHILD_PROPERTIES = [
|
||||
|
||||
const validate = getJsonValidator(webhookSchema)
|
||||
|
||||
export default class Webhook {
|
||||
#webhook
|
||||
constructor(webhook) {
|
||||
export interface WebhookSchema {
|
||||
description: string
|
||||
summary: string
|
||||
requestBody?: {
|
||||
content: {
|
||||
'application/json': {
|
||||
schema: Record<string, any>
|
||||
}
|
||||
}
|
||||
}
|
||||
'x-github': {
|
||||
'supported-webhook-types': string[]
|
||||
subcategory: string
|
||||
}
|
||||
}
|
||||
|
||||
interface WebhookInterface {
|
||||
descriptionHtml: string
|
||||
summaryHtml: string
|
||||
bodyParameters: TransformedParam[]
|
||||
availability: string[]
|
||||
action: string | null
|
||||
category: string
|
||||
process(): Promise<void>
|
||||
renderDescription(): Promise<this>
|
||||
renderBodyParameterDescriptions(): Promise<void>
|
||||
}
|
||||
|
||||
export default class Webhook implements WebhookInterface {
|
||||
#webhook: WebhookSchema
|
||||
descriptionHtml: string = ''
|
||||
summaryHtml: string = ''
|
||||
bodyParameters: TransformedParam[] = []
|
||||
availability: string[]
|
||||
action: string | null
|
||||
category: string
|
||||
|
||||
constructor(webhook: WebhookSchema) {
|
||||
this.#webhook = webhook
|
||||
this.descriptionHtml = ''
|
||||
this.summaryHtml = ''
|
||||
this.bodyParameters = []
|
||||
this.availability = webhook['x-github']['supported-webhook-types']
|
||||
this.action = get(
|
||||
webhook,
|
||||
@@ -45,30 +76,29 @@ export default class Webhook {
|
||||
// The OpenAPI uses hyphens for the webhook names, but the webhooks
|
||||
// are sent using underscores (e.g. `branch_protection_rule` instead
|
||||
// of `branch-protection-rule`)
|
||||
this.category = webhook['x-github'].subcategory.replaceAll('-', '_')
|
||||
return this
|
||||
this.category = webhook['x-github'].subcategory.replace(/-/g, '_')
|
||||
}
|
||||
|
||||
async process() {
|
||||
async process(): Promise<void> {
|
||||
await Promise.all([this.renderDescription(), this.renderBodyParameterDescriptions()])
|
||||
|
||||
const isValid = validate(this)
|
||||
const isValid = validate(this as WebhookInterface) // Add type assertion here
|
||||
if (!isValid) {
|
||||
console.error(JSON.stringify(validate.errors, null, 2))
|
||||
throw new Error(`Invalid OpenAPI webhook found: ${this.category}`)
|
||||
}
|
||||
}
|
||||
|
||||
async renderDescription() {
|
||||
async renderDescription(): Promise<this> {
|
||||
this.descriptionHtml = await renderContent(this.#webhook.description)
|
||||
this.summaryHtml = await renderContent(this.#webhook.summary)
|
||||
return this
|
||||
}
|
||||
|
||||
async renderBodyParameterDescriptions() {
|
||||
if (!this.#webhook.requestBody) return []
|
||||
const schema = get(this.#webhook, `requestBody.content.['application/json'].schema`, {})
|
||||
this.bodyParameters = isPlainObject(schema) ? await getBodyParams(schema, true, this.title) : []
|
||||
async renderBodyParameterDescriptions(): Promise<void> {
|
||||
if (!this.#webhook.requestBody) return
|
||||
const schema = get(this.#webhook, `requestBody.content['application/json'].schema`, {})
|
||||
this.bodyParameters = isPlainObject(schema) ? await getBodyParams(schema, true) : []
|
||||
|
||||
// Removes the children of the common properties
|
||||
this.bodyParameters.forEach((param) => {
|
||||
Reference in New Issue
Block a user