Skip to content

Commit

Permalink
removed compatibility layer, added basic auth sample into e2e manual …
Browse files Browse the repository at this point in the history
…test
  • Loading branch information
flamewow committed May 23, 2022
1 parent b11e8b4 commit eed0820
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 271 deletions.
65 changes: 46 additions & 19 deletions e2e/manual-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const host = 'localhost';
const docRelPath = '/api-docs';

const USE_FASTIFY = true;
const ENABLE_BASIC_AUTH = true;

const adapter = USE_FASTIFY ? new FastifyAdapter() : new ExpressAdapter();
const publicFolderPath = join(__dirname, '../../e2e', 'public');
Expand All @@ -26,6 +27,48 @@ async function bootstrap() {
ApplicationModule,
adapter
);
const httpAdapter = app.getHttpAdapter();

ENABLE_BASIC_AUTH &&
httpAdapter.use('/api-docs', (req, res, next) => {
function parse(input: string): { name: string; pass: string } {
const [, encodedPart] = input.split(' ');

const buff = Buffer.from(encodedPart, 'base64');
const text = buff.toString('ascii');
const [name, pass] = text.split(':');

return { name, pass };
}

function unauthorizedResponse(): void {
if (USE_FASTIFY) {
res.statusCode = 401;
res.setHeader('WWW-Authenticate', 'Basic');
} else {
res.status(401);
res.set('WWW-Authenticate', 'Basic');
}

next();
}

if (!req.headers.authorization) {
return unauthorizedResponse();
}

const credentials = parse(req.headers.authorization);

if (
!credentials ||
credentials?.name !== 'admin' ||
credentials?.pass !== 'admin'
) {
return unauthorizedResponse();
}

next();
});

app.setGlobalPrefix('/api/v1');

Expand Down Expand Up @@ -63,31 +106,15 @@ async function bootstrap() {
tryItOutEnabled: true
},
customfavIcon: '/public/favicon.ico',
customCssUrl: '/public/theme.css', // to showcase that in new implementation u can use custom css with fastify
uiHooks: USE_FASTIFY
? {
onRequest: (req: any, res: any, next: any) => {
console.log('FASTIFY HOOK POC 1');
next();
}
}
: undefined
customCssUrl: '/public/theme.css' // to showcase that in new implementation u can use custom css with fastify
});

SwaggerModule.setup('/swagger-docs', app, document, {
customSiteTitle: 'Demo API - Swagger UI 2',
uiConfig: {
swaggerOptions: {
persistAuthorization: true,
defaultModelsExpandDepth: -1
},
uiHooks: USE_FASTIFY
? {
onRequest: (req: any, res: any, next: any) => {
console.log('FASTIFY HOOK POC 2');
next();
}
}
: undefined
}
});

USE_FASTIFY
Expand Down
1 change: 1 addition & 0 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { ApplicationModule } from './src/app.module';
import { Cat } from './src/cats/classes/cat.class';
import { TagDto } from './src/cats/dto/tag.dto';
import { OpenAPI } from 'openapi-types';

describe('Validate OpenAPI schema', () => {
let app: INestApplication;
Expand Down
138 changes: 0 additions & 138 deletions lib/backward-compatilibity-layer.ts

This file was deleted.

1 change: 0 additions & 1 deletion lib/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export { OpenAPIObject } from './open-api-spec.interface';
export * from './swagger-custom-options.interface';
export * from './swagger-document-options.interface';
export * from './legacy-swagger-custom-options.interfaces';
72 changes: 0 additions & 72 deletions lib/interfaces/legacy-swagger-custom-options.interfaces.ts

This file was deleted.

0 comments on commit eed0820

Please sign in to comment.