Skip to content

Commit

Permalink
Merge pull request #30 from crazyfactory/29-basic-auth-endpoint
Browse files Browse the repository at this point in the history
29 basic auth endpoint
  • Loading branch information
wmathes authored Dec 20, 2017
2 parents 900296b + e6c9412 commit 2a93f6b
Show file tree
Hide file tree
Showing 9 changed files with 9,457 additions and 6,571 deletions.
253 changes: 156 additions & 97 deletions cli/index.js

Large diffs are not rendered by default.

12,868 changes: 9,240 additions & 3,628 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"format": "tsfmt -r ./sdk/TypeScript/src/sdk.ts",
"generate:typescript": "ts-node src/index.ts --lang=typescript",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"test": "nyc mocha --compilers ts:ts-node/register src/**/*.test.ts"
"test": "nyc mocha --compilers ts:ts-node/register \"src/**/*.test.ts\""
},
"nyc": {
"include": [
Expand Down Expand Up @@ -45,11 +45,11 @@
"awesome-typescript-loader": "^3.1.3",
"babel-core": "^6.24.1",
"chai": "^4.0.0",
"mocha": "^3.4.1",
"mocha": "^4.0.1",
"nyc": "^11.0.3",
"semantic-release": "^8.0.3",
"ts-node": "^3.0.4",
"typescript": "^2.3.2",
"typescript": "^2.6.2",
"webpack": "^3.0.0"
},
"dependencies": {
Expand Down
15 changes: 13 additions & 2 deletions src/GeneratorHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require("./StringHelpers");
import {GeneratorHelpers} from "./GeneratorHelpers";
import {expect} from 'chai';
import {IPathsData, IApiController, IApiMethod, HttpMethod} from "./interfaces";
import {SpecialParams} from "./SpecialParams";

describe("GeneratorHelpers", () => {
const paths: IPathsData = {
Expand Down Expand Up @@ -871,7 +872,12 @@ describe("GeneratorHelpers", () => {
expect(methods[11].httpMethod).to.equal(HttpMethod.DELETE);
});
it("returns correct method's allParams", () => {
expect(methods[0].allParams.length).to.equal(0);
// Basic Auth
expect(methods[0].allParams).to.deep.equal([{
name: SpecialParams.BASIC_AUTH,
in: "header"
}]);

expect(methods[10].allParams).to.deep.equal([
{
"name": "id",
Expand Down Expand Up @@ -910,14 +916,19 @@ describe("GeneratorHelpers", () => {
}]);
});
it("returns correct method's headerParams", () => {
expect(methods[2].headerParams.length).to.equal(0);
// Basic Auth
expect(methods[0].headerParams).to.deep.equal([{
name: SpecialParams.BASIC_AUTH,
in: "header"
}]);
expect(methods[1].headerParams).to.deep.equal([{
name: "Authorization",
in: "header",
type: "string",
description: "Refresh token",
required: true
}]);
expect(methods[2].headerParams.length).to.equal(0);
});
it("throws missing response", () => {
const localPaths: IPathsData = {
Expand Down
13 changes: 12 additions & 1 deletion src/GeneratorHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {HttpMethod, IApiMethod, IApiController, IPathsData} from "./interfaces";
import {ApiMethod} from "./models/ApiMethod";
import {SpecialParams} from "./SpecialParams";

export class GeneratorHelpers {
public static getApiControllers(emptyApiControllers: IApiController[], apiMethods: IApiMethod[]): IApiController[] {
Expand Down Expand Up @@ -67,7 +68,17 @@ export class GeneratorHelpers {
method.httpMethod = this.cleanHttpMethod(httpMethod);
method.allParams = paths[url][httpMethod].parameters;

for (let parameter of paths[url][httpMethod].parameters) {
if (paths[url][httpMethod].security && paths[url][httpMethod].security.length && paths[url][httpMethod].security[0].basic) {
method.allParams = [
...method.allParams,
{
name: SpecialParams.BASIC_AUTH,
in: "header"
}
]
}

for (let parameter of method.allParams) {
if (parameter.schema && parameter.schema["$ref"]) {
const ref = parameter.schema["$ref"];
parameter.schema = ref.substr(ref.lastIndexOf("/") + 1);
Expand Down
3 changes: 3 additions & 0 deletions src/SpecialParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export enum SpecialParams {
BASIC_AUTH = "BASIC_AUTH"
}
19 changes: 18 additions & 1 deletion src/TypeScriptGenerator/TsControllerGenerator.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require("../StringHelpers");
import {expect} from "chai";
import {SpecialParams} from "../SpecialParams";
import {TsControllerGenerator} from "./TsControllerGenerator";
import {HttpMethod, IApiMethod, ICode, IParams, IApiController} from "../interfaces";
import {ApiMethod} from "../models/ApiMethod";
Expand Down Expand Up @@ -305,6 +306,10 @@ describe("TsControllerGenerator", () => {
"description": "Refresh token",
"required": true
},
{
"name": SpecialParams.BASIC_AUTH,
"in": "header",
},
{
"name": "Content-Type",
"in": "header",
Expand All @@ -320,7 +325,7 @@ describe("TsControllerGenerator", () => {
"required": true
}
];
expect(TsControllerGenerator.getParamsDefinition(allParams)).to.equal(`{ id: number; data: IZones; authorization: string; contentType: string; search: string; }`);
expect(TsControllerGenerator.getParamsDefinition(allParams)).to.equal(`{ id: number; data: IZones; authorization: string; username: string; password: string; contentType: string; search: string; }`);
});
});

Expand Down Expand Up @@ -412,6 +417,18 @@ describe("TsControllerGenerator", () => {
expect(gen.getFetchRequestString(apiMethod)).to.equal(`{ headers: { "Authorization": params.authorization, "Content-Type": params.contentType } }`);
});

it("returns correct basic auth headerParams", () => {
const gen = new TsControllerGenerator([]);
const apiMethod: IApiMethod = new ApiMethod();
apiMethod.headerParams = [
{
"name": SpecialParams.BASIC_AUTH,
"in": "header"
}
];
expect(gen.getFetchRequestString(apiMethod)).to.equal(`{ headers: { "Authorization": "Basic " + btoa(params.username + ":" + params.password) } }`);
});

it("returns correct httpMethod", () => {
const gen = new TsControllerGenerator([]);
const apiMethod: IApiMethod = new ApiMethod();
Expand Down
15 changes: 12 additions & 3 deletions src/TypeScriptGenerator/TsControllerGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as fs from "fs";
import * as path from "path";
import {HttpMethod, IApiController, IApiMethod, ICode, IGenerator, IParams} from "../interfaces";
import {Code} from "../Code";
import {SpecialParams} from "../SpecialParams";
import {TypeScriptGenerator} from "./TypeScriptGenerator";

export class TsControllerGenerator implements IGenerator {
Expand Down Expand Up @@ -66,8 +67,12 @@ export class TsControllerGenerator implements IGenerator {
public static getParamsDefinition(params: IParams[], interfacePrefix: string = "I"): string {
let paramsDef: string = "{";
for (let param of params) {
if (param.type === "integer") param.type = "number";
paramsDef += ` ${param.name.toCamelCase()}: ${param.type || interfacePrefix + param.schema.toPascalCase()};`;
if (param.name === SpecialParams.BASIC_AUTH) {
paramsDef += ` username: string; password: string;`;
} else {
if (param.type === "integer") param.type = "number";
paramsDef += ` ${param.name.toCamelCase()}: ${param.type || interfacePrefix + param.schema.toPascalCase()};`;
}
}
paramsDef += " }";
return paramsDef;
Expand Down Expand Up @@ -101,7 +106,11 @@ export class TsControllerGenerator implements IGenerator {
if (apiMethod.headerParams.length) {
fetchRequest.headers = {};
for (let headerParam of apiMethod.headerParams) {
fetchRequest.headers[headerParam.name] = `params.${headerParam.name.toCamelCase()}`;
if (headerParam.name === SpecialParams.BASIC_AUTH) {
fetchRequest.headers["Authorization"] = `"Basic " + btoa(params.username + ":" + params.password)`;
} else {
fetchRequest.headers[headerParam.name] = `params.${headerParam.name.toCamelCase()}`;
}
}
}
if (apiMethod.httpMethod) {
Expand Down
Loading

0 comments on commit 2a93f6b

Please sign in to comment.