Skip to content

Commit

Permalink
feat: add convertCamelCaseFromSnake util
Browse files Browse the repository at this point in the history
  • Loading branch information
louis-lemon committed Jun 24, 2024
1 parent c1129df commit bc4ec51
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/token-storage/aws-storage.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LemonCredentials, LemonKMS, LemonOAuthToken, WebCoreConfig } from '../types';
import { REGION_KEY, TokenStorageService, USE_X_LEMON_IDENTITY_KEY } from './token-storage.service';
import { convertCamelCaseFromSnake } from '../utils';

export class AWSStorageService extends TokenStorageService {
private credentialKeys = [
Expand All @@ -26,9 +27,9 @@ export class AWSStorageService extends TokenStorageService {

async getAllItems() {
return await this.credentialKeys.reduce(async (promise, item) => {
const tmpResult: { [key: string]: string } = await promise.then();
tmpResult[`${this.prefix}.${item}`] = await this.storage.getItem(`${this.prefix}.${item}`);
return Promise.resolve(tmpResult);
const result: { [key: string]: string } = await promise.then();
result[`${this.prefix}.${item}`] = await this.storage.getItem(`${this.prefix}.${item}`);
return Promise.resolve(result);
}, Promise.resolve({}));
}

Expand Down Expand Up @@ -56,7 +57,7 @@ export class AWSStorageService extends TokenStorageService {
async getCachedOAuthToken(): Promise<LemonOAuthToken> {
const result: any = await this.credentialKeys.reduce(async (promise, item) => {
const tmp: { [key: string]: string } = await promise.then();
tmp[item] = await this.storage.getItem(`${this.prefix}.${item}`);
tmp[convertCamelCaseFromSnake(item)] = await this.storage.getItem(`${this.prefix}.${item}`);
return Promise.resolve(tmp);
}, Promise.resolve({}));

Expand Down
3 changes: 2 additions & 1 deletion src/token-storage/azure-storage.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TokenStorageService } from './token-storage.service';
import { LemonOAuthToken, WebCoreConfig } from '../types';
import { convertCamelCaseFromSnake } from '../utils';

/**
* A service to manage Azure-specific storage operations.
Expand Down Expand Up @@ -69,7 +70,7 @@ export class AzureStorageService extends TokenStorageService {
async getCachedOAuthToken(): Promise<LemonOAuthToken | null> {
const result: any = await this.credentialKeys.reduce(async (promise, item) => {
const tmp: { [key: string]: string } = await promise;
tmp[item] = await this.storage.getItem(`${this.prefix}.${item}`);
tmp[convertCamelCaseFromSnake(item)] = await this.storage.getItem(`${this.prefix}.${item}`);
return Promise.resolve(tmp);
}, Promise.resolve({}));

Expand Down
8 changes: 8 additions & 0 deletions src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ export const calcSignature = (
// return new Buffer(signature).toString('base64');
return signature;
};

export const convertCamelCaseFromSnake = (key: string) => {
return key
.toLowerCase()
.split('_')
.map((part, i) => (i > 0 ? part.charAt(0).toUpperCase() + part.slice(1) : part))
.join('');
};

0 comments on commit bc4ec51

Please sign in to comment.