Skip to content

Commit

Permalink
chore(api|api-rest): enable eslint and remove tslint (#13003)
Browse files Browse the repository at this point in the history
* chore(api): enable eslint and remove tslint

* chore(api): run yarn lint:fix

* chore(api): manual fix of errors reported by yarn lint

* chore(api-rest): enable eslint and remove tslint

* chore(api-reset): run yarn lint:fix

* chore(api-rest): manual fix of errors reported by yarn lint
  • Loading branch information
HuiSF authored Feb 15, 2024
1 parent 504c082 commit b735feb
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 189 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ module.exports = {
// will enable lint by packages
// 'adapter-nextjs',
// 'packages/analytics',
'packages/api',
// 'packages/api',
'packages/api-graphql',
'packages/api-rest',
// 'packages/api-rest',
'packages/auth',
// 'packages/aws-amplify',
// 'packages/core',
Expand Down
3 changes: 2 additions & 1 deletion packages/api-rest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"clean": "npm run clean:size && rimraf dist lib lib-esm",
"clean:size": "rimraf dual-publish-tmp tmp*",
"format": "echo \"Not implemented\"",
"lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
"lint": "eslint '**/*.{ts,tsx}' && npm run ts-coverage",
"lint:fix": "eslint '**/*.{ts,tsx}' --fix",
"ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 70.0"
},
"exports": {
Expand Down
18 changes: 10 additions & 8 deletions packages/api-rest/src/apis/common/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// SPDX-License-Identifier: Apache-2.0
import { AmplifyClassV6 } from '@aws-amplify/core';
import {
HttpRequest,
unauthenticatedHandler,
Headers,
HttpRequest,
authenticatedHandler,
getRetryDecider,
jitteredBackoff,
authenticatedHandler,
unauthenticatedHandler,
} from '@aws-amplify/core/internals/aws-client-utils';
import {
AWSCredentials,
Expand All @@ -28,10 +28,10 @@ type HandlerOptions = Omit<HttpRequest, 'body' | 'headers'> & {
withCredentials?: boolean;
};

type SigningServiceInfo = {
interface SigningServiceInfo {
service?: string;
region?: string;
};
}

/**
* Make REST API call with best-effort IAM auth.
Expand All @@ -46,7 +46,7 @@ type SigningServiceInfo = {
export const transferHandler = async (
amplify: AmplifyClassV6,
options: HandlerOptions & { abortSignal: AbortSignal },
signingServiceInfo?: SigningServiceInfo
signingServiceInfo?: SigningServiceInfo,
): Promise<RestApiResponse> => {
const { url, method, headers, body, withCredentials, abortSignal } = options;
const resolvedBody = body
Expand Down Expand Up @@ -88,6 +88,7 @@ export const transferHandler = async (
...baseOptions,
});
}

// Clean-up un-modeled properties from response.
return {
statusCode: response.statusCode,
Expand All @@ -98,11 +99,11 @@ export const transferHandler = async (

const iamAuthApplicable = (
{ headers }: HttpRequest,
signingServiceInfo?: SigningServiceInfo
signingServiceInfo?: SigningServiceInfo,
) => !headers.authorization && !headers['x-api-key'] && !!signingServiceInfo;

const resolveCredentials = async (
amplify: AmplifyClassV6
amplify: AmplifyClassV6,
): Promise<AWSCredentials | null> => {
try {
const { credentials } = await amplify.Auth.fetchAuthSession();
Expand All @@ -112,5 +113,6 @@ const resolveCredentials = async (
} catch (e) {
logger.debug('No credentials available, the request will be unsigned.');
}

return null;
};
17 changes: 11 additions & 6 deletions packages/api-rest/src/apis/common/internalPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import { AmplifyClassV6 } from '@aws-amplify/core';

import { InternalPostInput, RestApiResponse } from '../../types';
import { transferHandler } from './handler';
import { createCancellableOperation } from '../../utils';

import { transferHandler } from './handler';

/**
* This weak map provides functionality to cancel a request given the promise containing the `post` request.
*
Expand All @@ -25,7 +26,7 @@ const cancelTokenMap = new WeakMap<Promise<any>, AbortController>();
*/
export const post = (
amplify: AmplifyClassV6,
{ url, options, abortController }: InternalPostInput
{ url, options, abortController }: InternalPostInput,
): Promise<RestApiResponse> => {
const controller = abortController ?? new AbortController();
const responsePromise = createCancellableOperation(async () => {
Expand All @@ -37,14 +38,16 @@ export const post = (
...options,
abortSignal: controller.signal,
},
options?.signingServiceInfo
options?.signingServiceInfo,
);

return response;
}, controller);

const responseWithCleanUp = responsePromise.finally(() => {
cancelTokenMap.delete(responseWithCleanUp);
});

return responseWithCleanUp;
};

Expand All @@ -55,18 +58,20 @@ export const post = (
*/
export const cancel = (
promise: Promise<RestApiResponse>,
message?: string
message?: string,
): boolean => {
const controller = cancelTokenMap.get(promise);
if (controller) {
controller.abort(message);
if (message && controller.signal.reason !== message) {
// In runtimes where `AbortSignal.reason` is not supported, we track the reason ourselves.
// @ts-expect-error reason is read-only property.
controller.signal['reason'] = message;
controller.signal.reason = message;
}

return true;
}

return false;
};

Expand All @@ -75,7 +80,7 @@ export const cancel = (
*/
export const updateRequestToBeCancellable = (
promise: Promise<any>,
controller: AbortController
controller: AbortController,
) => {
cancelTokenMap.set(promise, controller);
};
35 changes: 19 additions & 16 deletions packages/api-rest/src/apis/common/publicApis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,44 @@
// SPDX-License-Identifier: Apache-2.0

import { AmplifyClassV6 } from '@aws-amplify/core';

import {
GetInput,
GetOperation,
PostInput,
PostOperation,
PutInput,
PutOperation,
ApiInput,
DeleteInput,
DeleteOperation,
GetInput,
GetOperation,
HeadInput,
HeadOperation,
PatchInput,
PatchOperation,
ApiInput,
PostInput,
PostOperation,
PutInput,
PutOperation,
RestApiOptionsBase,
} from '../../types';
import {
resolveApiUrl,
createCancellableOperation,
logger,
parseSigningInfo,
resolveApiUrl,
} from '../../utils';

import { transferHandler } from './handler';

const publicHandler = (
amplify: AmplifyClassV6,
options: ApiInput<RestApiOptionsBase>,
method: string
method: string,
) =>
createCancellableOperation(async abortSignal => {
const { apiName, options: apiOptions = {}, path: apiPath } = options;
const url = resolveApiUrl(
amplify,
apiName,
apiPath,
apiOptions?.queryParams
apiOptions?.queryParams,
);
const libraryConfigHeaders =
await amplify.libraryOptions?.API?.REST?.headers?.({
Expand All @@ -57,8 +59,9 @@ const publicHandler = (
method,
url,
headers,
`IAM signing options: ${JSON.stringify(signingServiceInfo)}`
`IAM signing options: ${JSON.stringify(signingServiceInfo)}`,
);

return transferHandler(
amplify,
{
Expand All @@ -68,7 +71,7 @@ const publicHandler = (
headers,
abortSignal,
},
signingServiceInfo
signingServiceInfo,
);
});

Expand All @@ -77,23 +80,23 @@ export const get = (amplify: AmplifyClassV6, input: GetInput): GetOperation =>

export const post = (
amplify: AmplifyClassV6,
input: PostInput
input: PostInput,
): PostOperation => publicHandler(amplify, input, 'POST');

export const put = (amplify: AmplifyClassV6, input: PutInput): PutOperation =>
publicHandler(amplify, input, 'PUT');

export const del = (
amplify: AmplifyClassV6,
input: DeleteInput
input: DeleteInput,
): DeleteOperation => publicHandler(amplify, input, 'DELETE');

export const head = (
amplify: AmplifyClassV6,
input: HeadInput
input: HeadInput,
): HeadOperation => publicHandler(amplify, input, 'HEAD');

export const patch = (
amplify: AmplifyClassV6,
input: PatchInput
input: PatchInput,
): PatchOperation => publicHandler(amplify, input, 'PATCH');
18 changes: 10 additions & 8 deletions packages/api-rest/src/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { Amplify } from '@aws-amplify/core';
import {
get as commonGet,
post as commonPost,
put as commonPut,
del as commonDel,
head as commonHead,
patch as commonPatch,
} from './common/publicApis';

import {
DeleteInput,
DeleteOperation,
Expand All @@ -26,6 +19,15 @@ import {
} from '../types';
import { RestApiError } from '../errors';

import {
del as commonDel,
get as commonGet,
head as commonHead,
patch as commonPatch,
post as commonPost,
put as commonPut,
} from './common/publicApis';

/**
* GET HTTP request
* @param {GetInput} input - Input for GET operation
Expand Down
31 changes: 16 additions & 15 deletions packages/api-rest/src/apis/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@ import {
AmplifyServer,
getAmplifyServerContext,
} from '@aws-amplify/core/internals/adapter-core';
import {
get as commonGet,
post as commonPost,
put as commonPut,
del as commonDel,
head as commonHead,
patch as commonPatch,
} from './common/publicApis';

import {
DeleteInput,
DeleteOperation,
Expand All @@ -29,6 +22,15 @@ import {
} from '../types';
import { RestApiError } from '../errors';

import {
del as commonDel,
get as commonGet,
head as commonHead,
patch as commonPatch,
post as commonPost,
put as commonPut,
} from './common/publicApis';

/**
* GET HTTP request (server-side)
* @param {AmplifyServer.ContextSpec} contextSpec - The context spec used to get the Amplify server context.
Expand All @@ -52,11 +54,10 @@ import { RestApiError } from '../errors';
* },
* });
* ```
* @see {@link clientGet}
*/
export const get = (
contextSpec: AmplifyServer.ContextSpec,
input: GetInput
input: GetInput,
): GetOperation =>
commonGet(getAmplifyServerContext(contextSpec).amplify, input);

Expand Down Expand Up @@ -86,7 +87,7 @@ export const get = (
*/
export const post = (
contextSpec: AmplifyServer.ContextSpec,
input: PostInput
input: PostInput,
): PostOperation =>
commonPost(getAmplifyServerContext(contextSpec).amplify, input);

Expand Down Expand Up @@ -116,7 +117,7 @@ export const post = (
*/
export const put = (
contextSpec: AmplifyServer.ContextSpec,
input: PutInput
input: PutInput,
): PutOperation =>
commonPut(getAmplifyServerContext(contextSpec).amplify, input);

Expand Down Expand Up @@ -145,7 +146,7 @@ export const put = (
*/
export const del = (
contextSpec: AmplifyServer.ContextSpec,
input: DeleteInput
input: DeleteInput,
): DeleteOperation =>
commonDel(getAmplifyServerContext(contextSpec).amplify, input);

Expand Down Expand Up @@ -174,7 +175,7 @@ export const del = (
*/
export const head = (
contextSpec: AmplifyServer.ContextSpec,
input: HeadInput
input: HeadInput,
): HeadOperation =>
commonHead(getAmplifyServerContext(contextSpec).amplify, input);

Expand Down Expand Up @@ -204,6 +205,6 @@ export const head = (
*/
export const patch = (
contextSpec: AmplifyServer.ContextSpec,
input: PatchInput
input: PatchInput,
): PatchOperation =>
commonPatch(getAmplifyServerContext(contextSpec).amplify, input);
1 change: 1 addition & 0 deletions packages/api-rest/src/errors/CanceledError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import { AmplifyErrorParams } from '@aws-amplify/core/internals/utils';

import { RestApiError } from './RestApiError';

/**
Expand Down
Loading

0 comments on commit b735feb

Please sign in to comment.