Skip to content

Commit

Permalink
refactoring & code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriiAndreiev committed Oct 10, 2024
1 parent 3952cf9 commit 7679e01
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 103 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-snippets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const { convert } = new MetricsSDKSnippet(
},
{
name: 'basic_auth',
default: 'default',
scheme: 'basic',
source: 'security',
type: 'http',
},
Expand Down
15 changes: 7 additions & 8 deletions packages/sdk-snippets/src/helpers/code-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import { CodeBuilder as HTTPSnippetCodeBuilder } from '@readme/httpsnippet/dist/

export type { CodeBuilderOptions };

export interface VariableOptions {
indentationLevel?: number;
name: string;
type: 'security' | 'server';
}

export class CodeBuilder extends HTTPSnippetCodeBuilder {
sections: {
payload?: {
Expand Down Expand Up @@ -43,14 +49,7 @@ export class CodeBuilder extends HTTPSnippetCodeBuilder {
* Push a dynamic piece of code to the snippet and record where in the snippet it was added.
*
*/
pushVariable = (
line: string,
opts: {
indentationLevel?: number;
name: string;
type: 'security' | 'server';
},
) => {
pushVariable = (line: string, opts: VariableOptions) => {
this.push(line, opts.indentationLevel);

// Record where in the snippet this variable is located.
Expand Down
50 changes: 31 additions & 19 deletions packages/sdk-snippets/src/targets/csharp/dotnet6/webhooks/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { VariableOptions } from '../../../../helpers/code-builder';
import type { Client } from '../../../targets';

import { CodeBuilder } from '../../../../helpers/code-builder';
Expand Down Expand Up @@ -92,31 +93,42 @@ export const dotnet6: Client = {
push('keys = new[]', 2);
push('{', 2);
security.forEach(data => {
push('new', 3);
push('{', 3);
pushVariable(`name = "${escapeForDoubleQuotes(data.name)}",`, {
const variableOptions: VariableOptions = {
type: 'security',
name: data.name,
indentationLevel: 4,
});
if (data.type === 'http') {
if (data.scheme === 'basic') {
push('user = "user",', 4);
push('pass = "pass"', 4);
} else if (data.scheme === 'bearer') {
push('apiKey = "apiKey"', 4);
};

push('new', 3);
push('{', 3);
if (data.type === 'http' || data.type === 'apiKey') {
pushVariable(`name = "${escapeForDoubleQuotes(data.name)}",`, variableOptions);

if (data.type === 'http') {
if (data.scheme === 'basic') {
push('user = "user",', 4);
push('pass = "pass",', 4);
} else if (data.scheme === 'bearer') {
pushVariable(
`bearer = "${escapeForDoubleQuotes(data.default || data.default === '' ? data.default : data.name)}",`,
variableOptions,
);
}
} else {
pushVariable(
`apiKey = "${escapeForDoubleQuotes(data.default || data.default === '' ? data.default : data.name)}",`,
variableOptions,
);
}
} else if (data.type === 'apiKey') {
push('apiKey = "apiKey"', 4);
} else {
pushVariable(
`${escapeForObjectKey(data.name)} = "${escapeForDoubleQuotes(
data.default || data.default === '' ? data.default : data.name,
)}",`,
variableOptions,
);
}

if (data.default) {
pushVariable(`options = "${escapeForDoubleQuotes(data.default)}",`, {
type: 'security',
name: data.name,
indentationLevel: 4,
});
}
push('},', 3);
});
push('}', 2);
Expand Down
49 changes: 30 additions & 19 deletions packages/sdk-snippets/src/targets/node/express/webhooks/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { VariableOptions } from '../../../../helpers/code-builder';
import type { Client } from '../../../targets';

import { CodeBuilder } from '../../../../helpers/code-builder';
Expand Down Expand Up @@ -80,29 +81,39 @@ export const express: Client = {
push('// OAS Security variables', 2);
push('keys: [', 2);
security.forEach(data => {
push('{', 3);
pushVariable(`name: '${escapeForSingleQuotes(data.name)}',`, {
const variableOptions: VariableOptions = {
type: 'security',
name: data.name,
indentationLevel: 4,
});
if (data.type === 'http') {
if (data.scheme === 'basic') {
push("user: 'user',", 4);
push("pass: 'pass',", 4);
} else if (data.scheme === 'bearer') {
push("apiKey: 'apiKey',", 4);
}
} else if (data.type === 'apiKey') {
push("apiKey: 'apiKey',", 4);
}
};

if (data.default) {
pushVariable(`default: '${escapeForSingleQuotes(data.default)}',`, {
type: 'security',
name: data.name,
indentationLevel: 4,
});
push('{', 3);
if (data.type === 'http' || data.type === 'apiKey') {
pushVariable(`name: '${escapeForSingleQuotes(data.name)}',`, variableOptions);

if (data.type === 'http') {
if (data.scheme === 'basic') {
push("user: 'user',", 4);
push("pass: 'pass',", 4);
} else if (data.scheme === 'bearer') {
pushVariable(
`bearer: '${escapeForSingleQuotes(data.default || data.default === '' ? data.default : data.name)}',`,
variableOptions,
);
}
} else {
pushVariable(
`apiKey: '${escapeForSingleQuotes(data.default || data.default === '' ? data.default : data.name)}',`,
variableOptions,
);
}
} else {
pushVariable(
`${escapeForObjectKey(data.name)}: '${escapeForSingleQuotes(
data.default || data.default === '' ? data.default : data.name,
)}',`,
variableOptions,
);
}
push('},', 3);
});
Expand Down
52 changes: 33 additions & 19 deletions packages/sdk-snippets/src/targets/php/laravel/webhooks/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { VariableOptions } from '../../../../helpers/code-builder';
import type { Client } from '../../../targets';

import { CodeBuilder } from '../../../../helpers/code-builder';
import { escapeForSingleQuotes } from '../../../../helpers/escape';
import { escapeForObjectKey, escapeForSingleQuotes } from '../../../../helpers/escape';

export const laravel: Client = {
info: {
Expand Down Expand Up @@ -78,30 +79,43 @@ export const laravel: Client = {
push('// OAS Security variables', 2);
push("'keys' => [", 2);
security.forEach(data => {
push('[', 3);
pushVariable(`'name' => '${escapeForSingleQuotes(data.name)}',`, {
const variableOptions: VariableOptions = {
type: 'security',
name: data.name,
indentationLevel: 4,
});
if (data.type === 'http') {
if (data.scheme === 'basic') {
push("'user' => 'user',", 4);
push("'pass' => 'pass',", 4);
} else if (data.scheme === 'bearer') {
push("'apiKey' => 'apiKey',", 4);
};

push('[', 3);
if (data.type === 'http' || data.type === 'apiKey') {
pushVariable(`'name' => '${escapeForSingleQuotes(data.name)}',`, variableOptions);

if (data.type === 'http') {
if (data.scheme === 'basic') {
push("'user' => 'user',", 4);
push("'pass' => 'pass',", 4);
} else if (data.scheme === 'bearer') {
pushVariable(
`'bearer' => '${escapeForSingleQuotes(
data.default || data.default === '' ? data.default : data.name,
)}',`,
variableOptions,
);
}
} else {
pushVariable(
`'apiKey' => '${escapeForSingleQuotes(data.default || data.default === '' ? data.default : data.name)}',`,
variableOptions,
);
}
} else if (data.type === 'apiKey') {
push("'apiKey' => 'apiKey',", 4);
} else {
pushVariable(
`'${escapeForObjectKey(data.name)}': '${escapeForSingleQuotes(
data.default || data.default === '' ? data.default : data.name,
)}',`,
variableOptions,
);
}

if (data.default) {
pushVariable(`'default' => '${escapeForSingleQuotes(data.default)}'`, {
type: 'security',
name: data.name,
indentationLevel: 4,
});
}
push('],', 3);
});
push(']', 2);
Expand Down
50 changes: 31 additions & 19 deletions packages/sdk-snippets/src/targets/python/flask/webhooks/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { VariableOptions } from '../../../../helpers/code-builder';
import type { Client } from '../../../targets';

import { CodeBuilder } from '../../../../helpers/code-builder';
import { escapeForDoubleQuotes } from '../../../../helpers/escape';
import { escapeForDoubleQuotes, escapeForObjectKey } from '../../../../helpers/escape';

export const flask: Client = {
info: {
Expand Down Expand Up @@ -92,30 +93,41 @@ export const flask: Client = {
push('# OAS Security variables', 3);
push('"keys": [', 3);
security.forEach(data => {
push('{', 4);
pushVariable(`"name": "${escapeForDoubleQuotes(data.name)}",`, {
const variableOptions: VariableOptions = {
type: 'security',
name: data.name,
indentationLevel: 5,
});
if (data.type === 'http') {
if (data.scheme === 'basic') {
push('"user": "user",', 5);
push('"pass": "pass",', 5);
} else if (data.scheme === 'bearer') {
push('"apiKey": "apiKey",', 5);
};

push('{', 4);
if (data.type === 'http' || data.type === 'apiKey') {
pushVariable(`"name": "${escapeForDoubleQuotes(data.name)}",`, variableOptions);

if (data.type === 'http') {
if (data.scheme === 'basic') {
push('"user": "user",', 5);
push('"pass": "pass",', 5);
} else if (data.scheme === 'bearer') {
pushVariable(
`"bearer": "${escapeForDoubleQuotes(data.default || data.default === '' ? data.default : data.name)}",`,
variableOptions,
);
}
} else {
pushVariable(
`"apiKey": "${escapeForDoubleQuotes(data.default || data.default === '' ? data.default : data.name)}",`,
variableOptions,
);
}
} else if (data.type === 'apiKey') {
push('"apiKey": "apiKey",', 5);
} else {
pushVariable(
`"${escapeForObjectKey(data.name)}": "${escapeForDoubleQuotes(
data.default || data.default === '' ? data.default : data.name,
)}",`,
variableOptions,
);
}

if (data.default) {
pushVariable(`"default": "${escapeForDoubleQuotes(data.default)}",`, {
type: 'security',
name: data.name,
indentationLevel: 5,
});
}
push('},', 4);
});
push(']', 3);
Expand Down
48 changes: 30 additions & 18 deletions packages/sdk-snippets/src/targets/ruby/rails/webhooks/client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { VariableOptions } from '../../../../helpers/code-builder';
import type { Client } from '../../../targets';

import { CodeBuilder } from '../../../../helpers/code-builder';
Expand Down Expand Up @@ -90,30 +91,41 @@ export const rails: Client = {
push('# OAS Security variables', 3);
push('keys: [', 3);
security.forEach(data => {
push('{', 4);
pushVariable(`name: '${escapeForSingleQuotes(data.name)}',`, {
const variableOptions: VariableOptions = {
type: 'security',
name: data.name,
indentationLevel: 5,
});
if (data.type === 'http') {
if (data.scheme === 'basic') {
push("user: 'user',", 5);
push("pass: 'pass',", 5);
} else if (data.scheme === 'bearer') {
push("apiKey: 'apiKey',", 5);
};

push('{', 4);
if (data.type === 'http' || data.type === 'apiKey') {
pushVariable(`name: '${escapeForSingleQuotes(data.name)}',`, variableOptions);

if (data.type === 'http') {
if (data.scheme === 'basic') {
push("user: 'user',", 5);
push("pass: 'pass',", 5);
} else if (data.scheme === 'bearer') {
pushVariable(
`bearer: '${escapeForSingleQuotes(data.default || data.default === '' ? data.default : data.name)}',`,
variableOptions,
);
}
} else {
pushVariable(
`apiKey: '${escapeForSingleQuotes(data.default || data.default === '' ? data.default : data.name)}',`,
variableOptions,
);
}
} else if (data.type === 'apiKey') {
push("apiKey: 'apiKey',", 5);
} else {
pushVariable(
`${escapeForObjectKey(data.name)}: '${escapeForSingleQuotes(
data.default || data.default === '' ? data.default : data.name,
)}',`,
variableOptions,
);
}

if (data.default) {
pushVariable(`default: '${escapeForSingleQuotes(data.default)}',`, {
type: 'security',
name: data.name,
indentationLevel: 5,
});
}
push('},', 4);
});
push(']', 3);
Expand Down

0 comments on commit 7679e01

Please sign in to comment.