Skip to content

Commit

Permalink
fix(postman): introduce support for v2.0.0 auth object (#251)
Browse files Browse the repository at this point in the history
fixes #250
  • Loading branch information
ostridm authored Aug 22, 2024
1 parent ce551fe commit e24dd94
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
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"
}
]

0 comments on commit e24dd94

Please sign in to comment.