Skip to content

Commit

Permalink
feat: rewrite index module in ts
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Zuev committed Dec 15, 2021
1 parent 55bf685 commit 99e8ba8
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 131 deletions.
8 changes: 7 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
"newline-after-var": "error",
"newline-before-return": "error",
"no-plusplus": "off",
"unicorn/import-style": "off"
"unicorn/import-style": "off",
"@typescript-eslint/no-var-requires": "off",
"no-underscore-dangle": ["error", {
"allowAfterThis": true,
"allowAfterSuper": false
}],
"unicorn/no-null": "off"
}
}
2 changes: 2 additions & 0 deletions src/api/iam/v1/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ export class IamTokenService {
* Creates an IAM token for the specified identity.
*/
create(request: CreateIamTokenRequest): Promise<CreateIamTokenResponse>;

static makeGrpcConstructor(): any;
}

export interface CreateIamTokenRequest {
Expand Down
37 changes: 0 additions & 37 deletions src/index.d.ts

This file was deleted.

93 changes: 0 additions & 93 deletions src/index.js

This file was deleted.

111 changes: 111 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import grpc, { ChannelCredentials } from 'grpc';
import util from './lib/util';
import { EndpointResolver } from './lib/endpoint';
import metadata from './lib/metadata';
import iam from './api/iam/v1';

type TokenCreator = () => Promise<string>;

interface ClientClass<T> {
new(address: string, credentials: ChannelCredentials, options?: object, tokenCreator?: TokenCreator): T;
__endpointId: string;
}

interface GenericConfig {
pollInterval?: number;
}

export interface OAuthCredentialsConfig extends GenericConfig {
oauthToken: string;
}

export interface IamTokenCredentialsConfig extends GenericConfig {
iamToken: string;
}

export type SessionConfig = OAuthCredentialsConfig | IamTokenCredentialsConfig | GenericConfig;

const createIamToken = async (iamEndpoint: string, req: unknown) => {
const Ctor = iam.IamTokenService.makeGrpcConstructor();
let client = new Ctor(iamEndpoint, grpc.credentials.createSsl());

client = util.pimpServiceInstance(client);
const resp = await client.create(req);

return resp.iamToken;
};

const newTokenCreator = (config: SessionConfig, iamEndpoint: string): TokenCreator => {
if ('oauthToken' in config) {
return () => createIamToken(iamEndpoint, {
yandexPassportOauthToken: config.oauthToken,
});
}

if ('iamToken' in config) {
return async () => config.iamToken;
}

const tokenService = new metadata.TokenService();

return async () => {
await tokenService.initialize();

return tokenService.getToken();
};
};

const newChannelCredentials = (tokenCreator: TokenCreator) => grpc.credentials.combineChannelCredentials(
grpc.credentials.createSsl(),
grpc.credentials.createFromMetadataGenerator((params, callback) => {
tokenCreator()
.then((token) => {
const md = new grpc.Metadata();

md.set('authorization', `Bearer ${token}`);

return callback(null, md);
})
.catch((error) => callback(error));
}),
);

export class Session {
private static defaultConfig: Partial<SessionConfig> = {
pollInterval: 1000,
};

private readonly __config: SessionConfig;
private readonly __endpointResolver: EndpointResolver;
private readonly __tokenCreator: TokenCreator;
private readonly __channelCredentials: ChannelCredentials;

constructor(config: SessionConfig) {
this.__config = {
...Session.defaultConfig,
...config,
};
this.__endpointResolver = new EndpointResolver();
this.__tokenCreator = newTokenCreator(
this.__config,
this.__endpointResolver.resolve('iam'),
);
this.__channelCredentials = newChannelCredentials(this.__tokenCreator);
}

async setEndpoint(newEndpoint: string) {
await this.__endpointResolver.updateEndpointList(newEndpoint);
}

client<T>(Cls: ClientClass<T>): T {
return util.pimpServiceInstance(
new Cls(
// eslint-disable-next-line no-underscore-dangle
this.__endpointResolver.resolve(Cls.__endpointId),
this.__channelCredentials,
undefined,
this.__tokenCreator,
),
);
}
}

0 comments on commit 99e8ba8

Please sign in to comment.