Skip to content

Commit

Permalink
chore: update OpenAPI script
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostrider-05 committed Dec 9, 2024
1 parent 1e28615 commit f2346db
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 19 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

Typescript Oauth library for the [V2 Patreon API](https://docs.patreon.com/) with:

- Support for Creator access tokens and Oauth tokens.
- Support for Creator access tokens and Oauth tokens
- Client with methods for calling every endpoint, both resource and webhook endpoints
- Methods to create a webhook server
- Typescript types that strongly reflect your query for raw or normalized responses.
- Typescript types that strongly reflect your query for raw or normalized responses

```ts
const query = buildQuery.campaign([])({ campaign: ['title']})
Expand Down Expand Up @@ -76,7 +76,7 @@ Tip: the Patreon API blocks browser requests, so choose a backend to handle Oaut

### OpenAPI schema

This library is used to create the Patreon OpenAPI for the [`patreon-api-spec`](https://github.com/ghostrider-05/patreon-api-spec) repository. The stable spec is also available on the [API documentation](https://patreon-api.pages.dev/api/) of this library.
This library is used to create the Patreon OpenAPI schemas for the [`patreon-api-spec`](https://github.com/ghostrider-05/patreon-api-spec) repository.

## Examples

Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default defineConfig({
themeConfig: {
nav: [
shared.createGuideItem(false),
{ text: 'API', link: '/api/index.html', activeMatch: '/api/' },
{ text: 'API Reference', link: 'https://patreon.apidocumentation.com/v2-stable/reference', },
shared.createLinksItem({
branch,
bugsUrl: bugs.url,
Expand Down
2 changes: 1 addition & 1 deletion docs/.vitepress/theme/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default {
DefaultTheme.enhanceApp(ctx)
ctx.app.use(createVuetify())

const spec = await fetchOpenAPISchema('openapi-schema')
const spec = await fetchOpenAPISchema()

// @ts-expect-error Unused variable
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ features:
linkText: See runtime requirements
- title: OpenAPI schema
details: Use the Patreon API with popular OpenAPI tools
link: /api/
link: https://patreon.apidocumentation.com/v2-stable/reference
linkText: Explore the API
- title: Simplified payloads
details: Don't want to deal with the JSON:API? Use the simplified payloads
Expand Down
15 changes: 10 additions & 5 deletions src/schemas/v2/api/components/headers.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { PATREON_RESPONSE_HEADERS, WebhookClient } from '../../../../v2'
import { ResponseHeaders, WebhookClient } from '../../../../v2'

export default {
[PATREON_RESPONSE_HEADERS.UUID]: {
[ResponseHeaders.UUID]: {
schema: {
type: 'string',
},
},
[PATREON_RESPONSE_HEADERS.Sha]: {
[ResponseHeaders.Sha]: {
schema: {
type: 'string',
},
},
[PATREON_RESPONSE_HEADERS.CfCacheStatus]: {
[ResponseHeaders.CfCacheStatus]: {
schema: {
type: 'string',
},
},
[PATREON_RESPONSE_HEADERS.CfRay]: {
[ResponseHeaders.CfRay]: {
schema: {
type: 'string',
},
},
[ResponseHeaders.RetryAfter]: {
schema: {
type: 'string',
},
Expand Down
4 changes: 3 additions & 1 deletion src/schemas/v2/api/components/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ export default {
required: true,
schema: {
type: 'string',
default: new RestClient().userAgent,
examples: [
new RestClient().userAgent,
],
}
},
}
5 changes: 5 additions & 0 deletions src/schemas/v2/api/paths/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export default [
id: 'editWebhook',
body: patchWebhookBody,
},
{
method: RequestMethod.Delete,
id: 'deleteWebhook',
experimental: true,
},
],
params: {
id: 'id',
Expand Down
1 change: 1 addition & 0 deletions src/schemas/v2/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface Route {
body?: Record<string, unknown>
deprecated?: boolean
description?: string
experimental?: boolean
scopes?: PatreonOauthScope[]
summary?: string
}[]
Expand Down
18 changes: 12 additions & 6 deletions src/scripts/v2/openapi/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { PatreonOauthScope } from '../../../rest/v2'
import { writeOpenAPISchema } from './schema'
import { getJsDocDescription, getTypes } from '../shared'

const baseAPIPath = '/api/oauth2/v2'

// eslint-disable-next-line jsdoc/require-jsdoc
function getScopes () {
const scopeEnum = getTypes('./src/rest/v2/oauth2/scopes.ts')
Expand All @@ -27,8 +29,13 @@ function getScopes () {

}

// @ts-expect-error unused preview for now
// eslint-disable-next-line @typescript-eslint/no-unused-vars
// eslint-disable-next-line jsdoc/require-jsdoc
function getDocumentationUrl (route: string, method: string) {
return 'https://docs.patreon.com/#'
+ method.toLowerCase()
+ (baseAPIPath + route).replace(/\//g, '-')
}

export default (fileName: string, preview?: boolean) => writeOpenAPISchema({
fileName,
spacing: 2,
Expand All @@ -41,8 +48,9 @@ export default (fileName: string, preview?: boolean) => writeOpenAPISchema({
}
},
paths: {
base: '/api/oauth/v2',
base: baseAPIPath,
routes,
includeExperimentalRoutes: !!preview,
formatId: (id) => `{${id ?? 'id'}}`,
contentType: 'application/json',
parameters: [
Expand All @@ -53,9 +61,7 @@ export default (fileName: string, preview?: boolean) => writeOpenAPISchema({
return {
documentation: {
description: 'Official documentation',
url: `https://docs.patreon.com/#${method.toLowerCase()}-api-oauth2-v2${
route.route(route.params?.id ?? 'id').replace(/\//g, '-')
}`,
url: getDocumentationUrl(route.route(route.params?.id ?? 'id'), method),
},
responses: [
{
Expand Down
8 changes: 7 additions & 1 deletion src/scripts/v2/openapi/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface PathSchemaOptions {
base: string
routes: Route[]
contentType: string
includeExperimentalRoutes: boolean
formatId: (id: string | null) => string
parameters: (
| string
Expand Down Expand Up @@ -41,7 +42,12 @@ function createPaths (schema: PathSchemaOptions) {
return schema.routes.reduce((obj, path) => ({
...obj,
[schema.base + path.route(schema.formatId(path.params?.id ?? null))]: path.methods.reduce((options, data) => {
const { id, method, body } = data
const { id, method, body, experimental } = data

if (experimental && !schema.includeExperimentalRoutes) {
return options
}

const {
documentation: externalDocs,
responses: routeResponses,
Expand Down

0 comments on commit f2346db

Please sign in to comment.