Skip to content

feat(specs): add cURL snippets #3841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@actions/core": "1.11.1",
"@actions/exec": "1.1.1",
"@actions/io": "1.1.3",
"@har-sdk/oas": "2.10.0",
"@octokit/rest": "21.0.2",
"@types/express": "5.0.0",
"@types/folder-hash": "4.0.4",
Expand All @@ -43,6 +44,7 @@
"express": "4.21.0",
"folder-hash": "4.0.4",
"fs-extra": "11.2.0",
"httpsnippet": "3.0.1",
"js-yaml": "4.1.0",
"knip": "5.30.4",
"micromatch": "4.0.8",
Expand Down
32 changes: 31 additions & 1 deletion scripts/specs/format.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import fsp from 'fs/promises';

import oas2har from '@har-sdk/oas';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import oas2har from '@har-sdk/oas';
import { oas2har } from '@har-sdk/oas';

since you only use 1 function

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too based on their doc but seems like it's incorrect

SyntaxError: The requested module '@har-sdk/oas' does not provide an export named 'oas2har'

import { HarRequest, HTTPSnippet } from 'httpsnippet';
import yaml from 'js-yaml';

import { Cache } from '../cache.js';
import { GENERATORS, exists, run, toAbsolutePath } from '../common.js';
import { exists, GENERATORS, run, toAbsolutePath } from '../common.js';
import { createSpinner } from '../spinners.js';
import type { Spec } from '../types.js';

Expand Down Expand Up @@ -59,6 +61,7 @@ export async function transformBundle({
}

const bundledSpec = yaml.load(await fsp.readFile(bundledPath, 'utf8')) as Spec;
const harRequests = await oas2har.oas2har(bundledSpec as any, { includeVendorExamples: true });
const tagsDefinitions = bundledSpec.tags;
const snippetSamples = docs ? await transformSnippetsToCodeSamples(clientName) : ({} as SnippetSamples);

Expand Down Expand Up @@ -100,6 +103,33 @@ export async function transformBundle({
}
}

// skip custom path for cURL
if (pathKey !== '/{path}' && specMethod['x-codeSamples']) {
const harRequest = harRequests.find((baseHarRequest) =>
baseHarRequest.url.includes(pathKey.replace('{indexName}', 'ALGOLIA_INDEX_NAME')),
);

if (!harRequest?.headers) {
break;
}

for (const harRequestHeader of harRequest.headers) {
if (harRequestHeader.name === bundledSpec.components.securitySchemes.appId?.name) {
harRequestHeader.value = 'ALGOLIA_APPLICATION_ID';
}

if (harRequestHeader.name === bundledSpec.components.securitySchemes.apiKey?.name) {
harRequestHeader.value = 'ALGOLIA_API_KEY';
}
}

specMethod['x-codeSamples'].push({
lang: 'cURL',
label: 'curl',
source: `${new HTTPSnippet(harRequest as HarRequest).convert('shell', 'curl')}`,
});
}

if (!bundledSpec.paths[pathKey][method].tags) {
continue;
}
Expand Down
1 change: 1 addition & 0 deletions scripts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type Spec = {
paths: Path;
components: {
schemas: Record<string, any>;
securitySchemes: Partial<{ appId?: Record<string, any>; apiKey?: Record<string, any> }>;
};
};

Expand Down
4 changes: 2 additions & 2 deletions specs/common/parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ IndexName:
required: true
schema:
type: string
example: YourIndexName
example: ALGOLIA_INDEX_NAME

ObjectID:
name: objectID
Expand All @@ -24,7 +24,7 @@ Index:
required: true
schema:
type: string
example: YourIndexName
example: ALGOLIA_INDEX_NAME

StartDate:
in: query
Expand Down
2 changes: 1 addition & 1 deletion specs/insights/common/schemas/EventAttributes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ eventName:
index:
type: string
description: Index name (case-sensitive) to which the event's items belong.
example: YourIndexName
example: ALGOLIA_INDEX_NAME

userToken:
type: string
Expand Down
10 changes: 5 additions & 5 deletions specs/recommend/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,23 @@ servers:
- url: https://{appId}.algolianet.com
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
- url: https://{appId}-1.algolianet.com
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
- url: https://{appId}-2.algolianet.com
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
- url: https://{appId}-3.algolianet.com
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
- url: https://{appId}-dsn.algolianet.com
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
security:
- appId: []
apiKey: []
Expand Down
2 changes: 1 addition & 1 deletion specs/search/paths/keys/common/parameters.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ KeyString:
required: true
schema:
type: string
example: YourAPIKey
example: ALGOLIA_API_KEY
description: API key.
10 changes: 5 additions & 5 deletions specs/search/spec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,23 @@ servers:
- url: https://{appId}.algolia.net
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
- url: https://{appId}-1.algolianet.com
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
- url: https://{appId}-2.algolianet.com
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
- url: https://{appId}-3.algolianet.com
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
- url: https://{appId}-dsn.algolia.net
variables:
appId:
default: myAppId
default: ALGOLIA_APPLICATION_ID
security:
- appId: []
apiKey: []
Expand Down
Loading
Loading