Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/silver-actors-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@redredgroup/samsungknox-api": patch
---

Removed KnoxDefaultAxios and replaced it with Knox{PRODUCT_NAME}Instance to support more APIs.
4 changes: 2 additions & 2 deletions packages/knox-api/src/apis/ams/v1/users/access-token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { knoxDefaultAxios } from '~/utils';
import { knoxAmsInstance, knoxKcsInstance } from '~/utils';
import { BaseApiRequireArgs, BaseResponse } from '~/types';
import { KnoxRequestError } from '~/errors';
import { AccessTokenArgs, AccessTokenResponse } from './access-token.type';
Expand All @@ -9,7 +9,7 @@ export const requestAccessToken = async ({
validityForAccessTokenInMinutes,
region,
}: BaseApiRequireArgs<AccessTokenArgs>): Promise<BaseResponse<AccessTokenResponse>> => {
const axios = knoxDefaultAxios({ region });
const axios = knoxAmsInstance({ region });

try {
const { data } = await axios.post<AccessTokenResponse>('/ams/v1/users/accesstoken', {
Expand Down
55 changes: 2 additions & 53 deletions packages/knox-api/src/apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,9 @@
import { AxiosInstance } from 'axios';
import { knoxDefaultAxios } from '~/utils';
import { KCContentManagementApplication, KCContentManagementAsset, KCDevice, KCLicense, KCProfile } from './kcs';
import { ERROR_MESSAGES, KnoxRequestError } from '~/errors';

/**
* Functions Export
*
* functions are mostly suitable for making simple calls or when your code is working on top of functions.
* They can also be used independently without creating a class instance.
*/
export * from './custom';
export * from './ams';
export * from './kcs';

/**
* Class Export
*
* If you want to call a lot of APIs at once, or if you want to customize and use your own instances,
* create a class instance and use it.
*/
export class KnoxInstance {
public kcProfile: KCProfile;
public kcLicense: KCLicense;
public kcDevice: KCDevice;
public kcContentManagementApplication: KCContentManagementApplication;
public kcContentManagementAsset: KCContentManagementAsset;

private axios: AxiosInstance;
private knoxAccessToken: string | null = null;
private region: string | null = null;

constructor({ knoxAccessToken, region }: { knoxAccessToken: string; region: string }) {
if (!knoxAccessToken) {
throw new KnoxRequestError(0, ERROR_MESSAGES.KNOX_ACCESS_TOKEN_MISSING);
} else {
this.knoxAccessToken = knoxAccessToken;
}

if (!region) {
console.warn(
'region was not injected, so it was automatically set to the default region of US. It is recommended that you add region manually to avoid making calls to a different or unexpected region.',
);
this.region = 'US';
} else {
this.region = region.toLowerCase();
}

this.axios = knoxDefaultAxios({
region: this.region,
knoxAccessToken: this.knoxAccessToken,
});

this.kcProfile = new KCProfile(this.axios);
this.kcLicense = new KCLicense(this.axios);
this.kcDevice = new KCDevice(this.axios);
this.kcContentManagementApplication = new KCContentManagementApplication(this.axios);
this.kcContentManagementAsset = new KCContentManagementAsset(this.axios);
}
}
export * from './ams';
export * from './custom';
60 changes: 60 additions & 0 deletions packages/knox-api/src/apis/kcs/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,61 @@
import { AxiosInstance } from 'axios';
import { knoxKcsInstance } from '~/utils';
import { KCContentManagementApplication, KCContentManagementAsset, KCDevice, KCLicense, KCProfile } from './v1';
import { ERROR_MESSAGES, KnoxRequestError } from '~/errors';

/**
* Functions Export
*
* functions are mostly suitable for making simple calls or when your code is working on top of functions.
* They can also be used independently without creating a class instance.
*/
export * from './v1';

/**
* Knox Configure (kc) Instance
*
* Class Export
*
* If you want to call a lot of APIs at once, or if you want to customize and use your own instances,
* create a class instance and use it.
*/
export class KcInstance {
public v1: {
Profile: KCProfile;
License: KCLicense;
Device: KCDevice;
ContentManagementApplication: KCContentManagementApplication;
ContentManagementAsset: KCContentManagementAsset;
};

private axios: AxiosInstance;
private knoxAccessToken: string | null = null;
private region: string | null = null;

constructor({ knoxAccessToken, region }: { knoxAccessToken: string; region: string }) {
if (!knoxAccessToken) {
throw new KnoxRequestError(0, ERROR_MESSAGES.KNOX_ACCESS_TOKEN_MISSING);
} else {
this.knoxAccessToken = knoxAccessToken;
}

if (!region) {
throw new KnoxRequestError(0, ERROR_MESSAGES.KNOX_REGION_MISSING);
} else {
this.region = region.toLowerCase();
}

this.axios = knoxKcsInstance({
region: this.region,
knoxAccessToken: this.knoxAccessToken,
});

this.v1 = {
Profile: new KCProfile(this.axios),
License: new KCLicense(this.axios),
Device: new KCDevice(this.axios),
ContentManagementApplication: new KCContentManagementApplication(this.axios),
ContentManagementAsset: new KCContentManagementAsset(this.axios),
};
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import { ERROR_MESSAGES, KnoxRequestError } from '~/errors';
import { kcCreateApplicationProfile } from './create-application-profile';
import { KnoxInstance } from '~/apis';
import { KcInstance } from '~/apis';

describe('POST /kcs/v1/kc/applications/profile Test', () => {
it('X-KNOX_APITOKEN missing', async () => {
Expand Down Expand Up @@ -63,12 +63,12 @@ describe('CLASS POST /kcs/v1/kc/applications/profile Test', () => {
let hasError = false;

try {
const instance = new KnoxInstance({
const instance = new KcInstance({
knoxAccessToken: '',
region: 'US',
});

await instance.kcContentManagementApplication.createApplicationProfile({
await instance.v1.ContentManagementApplication.createApplicationProfile({
args: {
additionalEula: {
termsAndConditions: '',
Expand All @@ -93,12 +93,12 @@ describe('CLASS POST /kcs/v1/kc/applications/profile Test', () => {
let hasError = false;

try {
const instance = new KnoxInstance({
const instance = new KcInstance({
knoxAccessToken: '',
region: 'TEST',
});

await instance.kcContentManagementApplication.createApplicationProfile({
await instance.v1.ContentManagementApplication.createApplicationProfile({
args: {
additionalEula: {
termsAndConditions: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BaseXApiRequire, BaseApiRequireArgs, BaseArgsInput, BaseResponse } from '~/types';
import { KCCreateApplicationProfileArgs, KCCreateApplicationProfileResponse } from './create-application-profile.type';
import { knoxDefaultAxios } from '~/utils';
import { knoxKcsInstance } from '~/utils';
import { AxiosInstance } from 'axios';
import { KnoxRequestError } from '~/errors';

export const kcCreateApplicationProfile = async (
value: BaseXApiRequire<BaseApiRequireArgs<BaseArgsInput<KCCreateApplicationProfileArgs>>>,
): Promise<BaseResponse<KCCreateApplicationProfileResponse>> => {
const { region, knoxAccessToken, args } = value;
const axios = knoxDefaultAxios({ region, knoxAccessToken });
const axios = knoxKcsInstance({ region, knoxAccessToken });
return request({ args, axios });
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import { ERROR_MESSAGES, KnoxRequestError } from '~/errors';
import { kcDeleteApplicationVersions } from './delete-application-versions';
import { KnoxInstance } from '~/apis';
import { KcInstance } from '~/apis';

describe('DELETE /kcs/v1/kc/applications/{applicationId} Test', () => {
it('X-KNOX_APITOKEN missing', async () => {
Expand Down Expand Up @@ -55,12 +55,12 @@ describe('CLASS POST /kcs/v1/kc/applications/profile Test', () => {
let hasError = false;

try {
const instance = new KnoxInstance({
const instance = new KcInstance({
knoxAccessToken: '',
region: 'US',
});

await instance.kcContentManagementApplication.deleteApplicationVersions({
await instance.v1.ContentManagementApplication.deleteApplicationVersions({
args: {
applicationId: 'TEST_APP',
},
Expand All @@ -81,12 +81,12 @@ describe('CLASS POST /kcs/v1/kc/applications/profile Test', () => {
let hasError = false;

try {
const instance = new KnoxInstance({
const instance = new KcInstance({
knoxAccessToken: 'asd',
region: 'TEST',
});

await instance.kcContentManagementApplication.deleteApplicationVersions({
await instance.v1.ContentManagementApplication.deleteApplicationVersions({
args: {
applicationId: 'TEST_APP',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BaseApiRequireArgs, BaseArgsInput, BaseResponse, BaseXApiRequire } from '~/types';
import { KCDeleteApplicationVersionsResponse, KCDeleteApplicationVersionsArgs } from './delete-application-versions.type';
import { knoxDefaultAxios } from '~/utils';
import { knoxKcsInstance } from '~/utils';
import { KnoxRequestError } from '~/errors';
import { AxiosInstance } from 'axios';

export const kcDeleteApplicationVersions = async (
value: BaseXApiRequire<BaseApiRequireArgs<BaseArgsInput<KCDeleteApplicationVersionsArgs>>>,
): Promise<BaseResponse<KCDeleteApplicationVersionsResponse>> => {
const { region, knoxAccessToken, args } = value;
const axios = knoxDefaultAxios({ region, knoxAccessToken });
const axios = knoxKcsInstance({ region, knoxAccessToken });
return request({ args, axios });
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BaseXApiRequire, BaseApiRequireArgs, BaseArgsInput, BaseResponse } from '~/types';
import { KCGetApplicationResponse, KCGetApplicationsArgs } from './get-applications.type';
import { knoxDefaultAxios } from '~/utils';
import { knoxKcsInstance } from '~/utils';
import { KnoxRequestError } from '~/errors';
import { AxiosInstance } from 'axios';

export const kcGetApplications = async (
value: BaseXApiRequire<BaseApiRequireArgs<BaseArgsInput<KCGetApplicationsArgs>>>,
): Promise<BaseResponse<KCGetApplicationResponse>> => {
const { region, knoxAccessToken, args } = value;
const axios = knoxDefaultAxios({ region, knoxAccessToken });
const axios = knoxKcsInstance({ region, knoxAccessToken });
return request({ args, axios });
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { ERROR_MESSAGES, KnoxRequestError } from '~/errors';
import { KnoxInstance, kcUploadAPublicAppFromPlayStoreOrGalaxyStore } from '~/apis';
import { KcInstance, kcUploadAPublicAppFromPlayStoreOrGalaxyStore } from '~/apis';

describe('POST /kcs/v1/kc/applications Test', () => {
it('X-KNOX_APITOKEN missing', async () => {
Expand Down Expand Up @@ -58,12 +58,12 @@ describe('CLASS POST /kcs/v1/kc/applications Test', () => {
let hasError = false;

try {
const instance = new KnoxInstance({
const instance = new KcInstance({
knoxAccessToken: '',
region: 'US',
});

await instance.kcContentManagementApplication.uploadAPublicAppFromPlayStoreOrGalaxyStore({
await instance.v1.ContentManagementApplication.uploadAPublicAppFromPlayStoreOrGalaxyStore({
args: {
name: '',
platform: 'ANDROID',
Expand All @@ -86,12 +86,12 @@ describe('CLASS POST /kcs/v1/kc/applications Test', () => {
let hasError = false;

try {
const instance = new KnoxInstance({
const instance = new KcInstance({
knoxAccessToken: 'asd',
region: 'TEST',
});

await instance.kcContentManagementApplication.uploadAPublicAppFromPlayStoreOrGalaxyStore({
await instance.v1.ContentManagementApplication.uploadAPublicAppFromPlayStoreOrGalaxyStore({
args: {
name: '',
platform: 'ANDROID',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {
KCUploadAPublicFromPlayStoreOrGalaxyStoreAppArgs,
KCUploadAPublicFromPlayStoreOrGalaxyStoreAppResponse,
} from './upload-a-public-app-from-playstore-or-galaxystore.type';
import { knoxDefaultAxios } from '~/utils';
import { knoxKcsInstance } from '~/utils';
import { AxiosInstance } from 'axios';
import { KnoxRequestError } from '~/errors';

export const kcUploadAPublicAppFromPlayStoreOrGalaxyStore = (
value: BaseXApiRequire<BaseApiRequireArgs<BaseArgsInput<KCUploadAPublicFromPlayStoreOrGalaxyStoreAppArgs>>>,
): Promise<BaseResponse<KCUploadAPublicFromPlayStoreOrGalaxyStoreAppResponse>> => {
const { region, knoxAccessToken, args } = value;
const axios = knoxDefaultAxios({ region, knoxAccessToken });
const axios = knoxKcsInstance({ region, knoxAccessToken });
return request({ args, axios });
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { ERROR_MESSAGES, KnoxRequestError } from '~/errors';
import { KnoxInstance, kcUploadAnInHouseApp } from '~/apis';
import { KcInstance, kcUploadAnInHouseApp } from '~/apis';

describe('POST /kcs/v1/kc/applications/upload Test', () => {
it('X-KNOX_APITOKEN missing', async () => {
Expand Down Expand Up @@ -56,12 +56,12 @@ describe('CLASS POST /kcs/v1/kc/applications/upload Test', () => {
let hasError = false;

try {
const instance = new KnoxInstance({
const instance = new KcInstance({
knoxAccessToken: '',
region: 'US',
});

await instance.kcContentManagementApplication.uploadAnInHouseApp({
await instance.v1.ContentManagementApplication.uploadAnInHouseApp({
args: {
name: '',
file: '',
Expand All @@ -83,12 +83,12 @@ describe('CLASS POST /kcs/v1/kc/applications/upload Test', () => {
let hasError = false;

try {
const instance = new KnoxInstance({
const instance = new KcInstance({
knoxAccessToken: 'asd',
region: 'TEST',
});

await instance.kcContentManagementApplication.uploadAnInHouseApp({
await instance.v1.ContentManagementApplication.uploadAnInHouseApp({
args: {
name: '',
file: '',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BaseXApiRequire, BaseApiRequireArgs, BaseArgsInput, BaseResponse } from '~/types';
import { KCUploadAnInHouseAppArgs, KCUploadAnInHouseAppResponse } from './upload-an-in-house-app.type';
import { knoxDefaultAxios } from '~/utils';
import { knoxKcsInstance } from '~/utils';
import { AxiosInstance } from 'axios';
import { KnoxRequestError } from '~/errors';

export const kcUploadAnInHouseApp = async (
value: BaseXApiRequire<BaseApiRequireArgs<BaseArgsInput<KCUploadAnInHouseAppArgs>>>,
): Promise<BaseResponse<KCUploadAnInHouseAppResponse>> => {
const { region, knoxAccessToken, args } = value;
const axios = knoxDefaultAxios({
const axios = knoxKcsInstance({
region,
knoxAccessToken,
customHeader: {
Expand Down
Loading