Skip to content

Commit

Permalink
[#271]: getAvatarImageByID updated for version2
Browse files Browse the repository at this point in the history
  • Loading branch information
MrRefactoring committed Oct 9, 2023
1 parent 09b5a97 commit 19d8e45
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/clients/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const ATLASSIAN_TOKEN_CHECK_FLAG = 'X-Atlassian-Token';
const ATLASSIAN_TOKEN_CHECK_NOCHECK_VALUE = 'no-check';

export class BaseClient implements Client {
public instance: AxiosInstance;
private instance: AxiosInstance;

constructor(protected readonly config: Config) {
try {
Expand Down
16 changes: 12 additions & 4 deletions src/version2/avatars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class Avatars {
* - For custom issue type avatars, _Browse projects_ [project permission](https://confluence.atlassian.com/x/yodKLg)
* for at least one project the issue type is used in.
*/
async getAvatarImageByID<T = unknown>(
async getAvatarImageByID<T = Models.AvatarWithDetails>(
parameters: Parameters.GetAvatarImageByID,
callback: Callback<T>,
): Promise<void>;
Expand All @@ -235,21 +235,29 @@ export class Avatars {
* - For custom issue type avatars, _Browse projects_ [project permission](https://confluence.atlassian.com/x/yodKLg)
* for at least one project the issue type is used in.
*/
async getAvatarImageByID<T = unknown>(parameters: Parameters.GetAvatarImageByID, callback?: never): Promise<T>;
async getAvatarImageByID<T = unknown>(
async getAvatarImageByID<T = Models.AvatarWithDetails>(parameters: Parameters.GetAvatarImageByID, callback?: never): Promise<T>;
async getAvatarImageByID<T = Models.AvatarWithDetails>(
parameters: Parameters.GetAvatarImageByID,
callback?: Callback<T>,
): Promise<void | T> {
const config: RequestConfig = {
url: `/rest/api/2/universal_avatar/view/type/${parameters.type}/avatar/${parameters.id}`,
method: 'GET',
responseType: 'arraybuffer',
params: {
size: parameters.size,
format: parameters.format,
},
};

return this.client.sendRequest(config, callback);
const {
data: avatar,
headers: { 'content-type': contentTypeWithEncoding },
} = await this.client.sendRequestFullResponse<T>(config);

const contentType = contentTypeWithEncoding.split(';')[0].trim();

return this.client.handleSuccessResponse({ contentType, avatar }, callback);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/version2/models/avatarWithDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface AvatarWithDetails {
/** The content type of the avatar. Expected values include 'image/png', 'image/svg+xml', or any other valid MIME type. */
contentType: 'image/png' | 'image/svg+xml' | string;
/** The binary representation of the avatar image. */
avatar: ArrayBuffer;
}
1 change: 1 addition & 0 deletions src/version2/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export * from './availableWorkflowTriggerTypes';
export * from './avatar';
export * from './avatars';
export * from './avatarUrls';
export * from './avatarWithDetails';
export * from './bulkChangeOwnerDetails';
export * from './bulkCreateCustomFieldOptionRequest';
export * from './bulkCustomFieldOptionCreateRequest';
Expand Down
2 changes: 1 addition & 1 deletion src/version2/parameters/getAvatarImageByID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export interface GetAvatarImageByID {
/** The icon type of the avatar. */
type: string;
/** The ID of the avatar. */
id: number;
id: number | string;
/** The size of the avatar image. If not provided the default size is returned. */
size?: 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge' | string;
/** The format to return the avatar image in. If not provided the original content format is returned. */
Expand Down
2 changes: 2 additions & 0 deletions src/version3/models/avatarWithDetails.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export interface AvatarWithDetails {
/** The content type of the avatar. Expected values include 'image/png', 'image/svg+xml', or any other valid MIME type. */
contentType: 'image/png' | 'image/svg+xml' | string;
/** The binary representation of the avatar image. */
avatar: ArrayBuffer;
}
2 changes: 1 addition & 1 deletion src/version3/models/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './avatarWithDetails';
export * from './actorInput';
export * from './actorsMap';
export * from './addField';
Expand Down Expand Up @@ -30,6 +29,7 @@ export * from './availableDashboardGadgetsResponse';
export * from './avatar';
export * from './avatars';
export * from './avatarUrls';
export * from './avatarWithDetails';
export * from './bulkChangeOwnerDetails';
export * from './bulkCreateCustomFieldOptionRequest';
export * from './bulkCustomFieldOptionCreateRequest';
Expand Down
22 changes: 22 additions & 0 deletions tests/integration/version2/avatars.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Avatar } from '../../../src/version3/models';
import { getVersion2Client } from '../utils';
import test from 'ava';

const client = getVersion2Client();

let avatar: Avatar | undefined;

test.serial('should get all system avatars', async t => {
const systemAvatars = await client.avatars.getAllSystemAvatars({ type: 'project' });

avatar = systemAvatars.system?.[0];

t.truthy(!!avatar);
});

test.serial('should return avatar image with contentType', async t => {
const avatarWithDetails = await client.avatars.getAvatarImageByID({ id: avatar!.id, type: 'project' });

t.is(avatarWithDetails.contentType, 'image/svg+xml');
t.truthy(avatarWithDetails.avatar instanceof Uint8Array);
});

0 comments on commit 19d8e45

Please sign in to comment.