-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add codemod for custom http headers (#369)
- Loading branch information
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { fileURLToPath } from 'node:url'; | ||
|
||
import fs from 'fs'; | ||
import path from 'path'; | ||
import { Project } from 'ts-morph'; | ||
|
||
const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
||
function addHttpClientHeaderHandler(project: Project) { | ||
const file = project.getSourceFileOrThrow('./src/http/client.ts'); | ||
const httpClient = file.getClassOrThrow('HttpClient'); | ||
const constructor = httpClient.getConstructors()[0]; | ||
|
||
const importPath = './handlers/mb-header-handler'; | ||
|
||
const existingImport = file.getImportDeclaration((dec) => { | ||
return dec.getModuleSpecifierValue() === importPath; | ||
}); | ||
|
||
if (!existingImport) { | ||
file.addImportDeclaration({ | ||
moduleSpecifier: importPath, | ||
namedImports: [{ name: 'HeaderHandler' }], | ||
}); | ||
} | ||
|
||
const newHandlerLine = 'this.requestHandlerChain.addHandler(new HeaderHandler());'; | ||
|
||
const alreadyHasHandler = constructor.getStatements().some((statement) => { | ||
return statement.getText() === newHandlerLine; | ||
}); | ||
|
||
if (!alreadyHasHandler) { | ||
constructor.insertStatements(0, newHandlerLine); | ||
} | ||
} | ||
|
||
function addHeaderHandlerFile(project: Project) { | ||
const sourceFilePath = path.join(__dirname, 'mods', 'mb-header-handler.ts'); | ||
const targetFilePath = './src/http/handlers/mb-header-handler.ts'; | ||
|
||
const contents = fs.readFileSync(sourceFilePath, { encoding: 'utf-8' }); | ||
const file = project.createSourceFile(targetFilePath, contents, { overwrite: true }); | ||
|
||
// fix import paths | ||
file.getImportDeclarations().forEach((dec) => { | ||
const moduleSpecifierValue = dec.getModuleSpecifierValue(); | ||
|
||
const newModuleSpecifierValue = path.relative( | ||
path.dirname(targetFilePath), | ||
path.resolve(path.dirname(sourceFilePath), moduleSpecifierValue), | ||
); | ||
|
||
dec.setModuleSpecifier(newModuleSpecifierValue); | ||
}); | ||
} | ||
|
||
function addHeadersToSdkConfig(project: Project) { | ||
const file = project.getSourceFileOrThrow('./src/http/types.ts'); | ||
const sdkConfig = file.getInterfaceOrThrow('SdkConfig'); | ||
|
||
if (!sdkConfig.getProperty('headers')) { | ||
sdkConfig.addProperty({ | ||
name: 'headers', | ||
type: 'Record<string, string>', | ||
hasQuestionToken: true, | ||
}); | ||
} | ||
} | ||
|
||
export async function applyCodeMods() { | ||
const project = new Project({ | ||
tsConfigFilePath: path.join(__dirname, '..', 'tsconfig.build.json'), | ||
}); | ||
|
||
addHeaderHandlerFile(project); | ||
addHttpClientHeaderHandler(project); | ||
addHeadersToSdkConfig(project); | ||
await project.save(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { SerializationStyle } from '../../src/http/serialization/base-serializer'; | ||
import { Request } from '../../src/http/transport/request'; | ||
import { HttpResponse, RequestHandler } from '../../src/http/types'; | ||
|
||
export class HeaderHandler implements RequestHandler { | ||
next?: RequestHandler; | ||
|
||
async handle<T>(request: Request<T>): Promise<HttpResponse<T>> { | ||
if (!this.next) { | ||
throw new Error(`No next handler set in ${HeaderHandler.name}`); | ||
} | ||
|
||
const requestWithHeaders = this.addHeaders(request); | ||
return this.next?.handle(requestWithHeaders); | ||
} | ||
|
||
private addHeaders<T>(request: Request<T>): Request<T> { | ||
const { headers } = request.config as any; | ||
if (typeof headers !== 'object') { | ||
return request; | ||
} | ||
|
||
for (const [key, value] of Object.entries(headers)) { | ||
request.addHeaderParam(key, { | ||
key: key, | ||
value: value, | ||
explode: false, | ||
encode: false, | ||
style: SerializationStyle.SIMPLE, | ||
}); | ||
} | ||
|
||
return request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters