Skip to content
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

fix(postman): introduce support for v2.0.0 auth object #251

Merged
merged 6 commits into from
Aug 22, 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
23 changes: 17 additions & 6 deletions packages/postman/src/converter/DefaultConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,16 +121,12 @@ export class DefaultConverter implements Converter {

/* istanbul ignore next */
private authRequest(request: Request, auth: Postman.RequestAuth): void {
const params: Postman.Variable[] | undefined = auth[auth.type];
const options = this.getAuthOptions(auth);

if (!params) {
if (!options) {
return;
}

const options = Object.fromEntries(
params.map((val: Postman.Variable) => [val.key, val.value].map(String))
);

switch (auth.type) {
case 'apikey':
this.apiKeyAuth(request, options);
Expand All @@ -150,6 +146,21 @@ export class DefaultConverter implements Converter {
}
}

private getAuthOptions(
auth: Postman.RequestAuth
): Record<string, string> | undefined {
const params: Postman.Variable[] | Record<string, string> | undefined =
auth[auth.type];

return Array.isArray(params)
? Object.fromEntries(
params.map((val: Postman.Variable) =>
[val.key, val.value].map(String)
)
)
: params;
}

/* istanbul ignore next */
private oauth2(request: Request, options: Record<string, string>): void {
if (!options.accessToken || options.tokenType === 'mac') {
Expand Down
5 changes: 5 additions & 0 deletions packages/postman/tests/DefaultConverter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ describe('DefaultConverter', () => {
});

it.each([
{
inputPath: './fixtures/v2.0.0-auth.postman_collection.json',
expectedPath: './fixtures/v2.0.0-auth.postman_collection.result.json',
label: 'v2.0.0 auth'
},
{
inputPath: './fixtures/trailing-slash.postman_collection.json',
expectedPath:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"info": {
"name": "Auth",
"schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
},
"item": [
{
"name": "users",
"item": [
{
"name": "Users",
"request": {
"auth": {
"type": "bearer",
"bearer": {
"token": "token-value"
}
},
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/json"
}
],
"url": "{{baseUrl}}/users"
},
"response": [
{
"name": "Success",
"originalRequest": {
"method": "GET",
"header": [
{
"key": "Accept",
"value": "application/json"
},
{
"key": "Authorization",
"value": "Bearer <token>",
"description": "Added as a part of security scheme: bearer"
}
],
"url": "{{baseUrl}}/user"
},
"status": "OK",
"code": 200,
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"cookie": [],
"body": "{\n \"username\": \"<string>\",\n \"name\": \"<string>\" \n}"
}
]
}
]
}
],
"variable": [
{
"key": "baseUrl",
"value": "https://example.com/api/v1"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
{
"bodySize": -1,
"cookies": [],
"headers": [
{
"name": "Accept",
"value": "application/json"
},
{
"name": "authorization",
"value": "Bearer token-value"
}
],
"headersSize": -1,
"httpVersion": "HTTP/1.1",
"method": "GET",
"queryString": [],
"url": "https://example.com/api/v1/users"
}
]
Loading