Skip to content

Commit

Permalink
ts strict mode updates
Browse files Browse the repository at this point in the history
  • Loading branch information
david-mcafee committed Nov 13, 2023
1 parent de5a0ca commit f0bbb18
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 36 deletions.
3 changes: 2 additions & 1 deletion packages/adapter-nextjs/src/api/generateServerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { NextServer } from '../types';
import { createServerRunnerForAPI } from './createServerRunnerForAPI';
import { getAmplifyConfig } from '../utils';
import { GraphQLAuthMode } from '@aws-amplify/core/internals/utils';
import { CustomHeaders } from '@aws-amplify/data-schema-types';

type CookiesClientParams = {
cookies: NextServer.ServerComponentContext['cookies'];
Expand Down Expand Up @@ -116,7 +117,7 @@ export function generateServerClientUsingReqRes<
const wrappedGraphql = (
contextSpec: AmplifyServer.ContextSpec,
options: GraphQLOptionsV6,
additionalHeaders?: { [key: string]: string }
additionalHeaders?: CustomHeaders
) => {
const amplifyInstance = getAmplifyServerContext(contextSpec).amplify;
return prevGraphql.call(
Expand Down
4 changes: 2 additions & 2 deletions packages/api-graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"typescript": "5.0.2",
"@rollup/plugin-typescript": "11.1.5",
"rollup": "3.29.4",
"@aws-amplify/data-schema": "^0.11.1"
"@aws-amplify/data-schema": "^0.12.0"
},
"files": [
"dist/cjs",
Expand All @@ -81,7 +81,7 @@
"dependencies": {
"@aws-amplify/core": "6.0.1",
"@aws-amplify/api-rest": "4.0.1",
"@aws-amplify/data-schema-types": "^0.5.0",
"@aws-amplify/data-schema-types": "^0.6.0",
"@aws-sdk/types": "3.387.0",
"graphql": "15.8.0",
"tslib": "^2.5.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/api-graphql/src/GraphQLAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { AmplifyClassV6 } from '@aws-amplify/core';
import { Category, ApiAction } from '@aws-amplify/core/internals/utils';
import { GraphQLOptions, GraphQLResult } from './types';
import { InternalGraphQLAPIClass } from './internals/InternalGraphQLAPI';
import { CustomHeaders } from '@aws-amplify/data-schema-types';
import { Observable } from 'rxjs';

export const graphqlOperation = (
Expand Down Expand Up @@ -34,7 +35,7 @@ export class GraphQLAPIClass extends InternalGraphQLAPIClass {
graphql<T = any>(
amplify: AmplifyClassV6 | (() => Promise<AmplifyClassV6>),
options: GraphQLOptions,
additionalHeaders?: { [key: string]: string }
additionalHeaders?: CustomHeaders
): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
return super.graphql(amplify, options, additionalHeaders, {
category: Category.API,
Expand Down
5 changes: 3 additions & 2 deletions packages/api-graphql/src/internals/APIClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ListArgs,
QueryArgs,
V6Client,
V6ClientSSRCookies,
V6ClientSSRRequest,
__authMode,
__authToken,
Expand Down Expand Up @@ -784,12 +785,12 @@ export function authModeParams(

/**
* Retrieves custom headers from either the client or request options.
* @param {client} V6Client - for extracting client headers
* @param {client} V6Client | V6ClientSSRRequest | V6ClientSSRCookies - for extracting client headers
* @param {requestHeaders} [CustomHeaders] - request headers
* @returns {CustomHeaders} - custom headers
*/
export function getCustomHeaders(
client: V6Client,
client: V6Client | ClientWithModels,
requestHeaders?: CustomHeaders
): CustomHeaders {
let headers: CustomHeaders = client[__headers] || {};
Expand Down
48 changes: 38 additions & 10 deletions packages/api-graphql/src/internals/InternalGraphQLAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
updateRequestToBeCancellable,
} from '@aws-amplify/api-rest/internals';
import { AWSAppSyncRealTimeProvider } from '../Providers/AWSAppSyncRealTimeProvider';
import { CustomHeaders } from '@aws-amplify/data-schema-types';
import { resolveConfig, resolveLibraryOptions } from '../utils';

const USER_AGENT_HEADER = 'x-amz-user-agent';
Expand Down Expand Up @@ -66,7 +67,7 @@ export class InternalGraphQLAPIClass {
private async _headerBasedAuth(
amplify: AmplifyClassV6,
authMode: GraphQLAuthMode,
additionalHeaders: { [key: string]: string } = {}
additionalHeaders: Record<string, string> | Headers = {}
) {
const {
region: region,
Expand Down Expand Up @@ -113,11 +114,19 @@ export class InternalGraphQLAPIClass {
}
break;
case 'lambda':
if (!additionalHeaders.Authorization) {
if (
(typeof additionalHeaders === 'object' &&
!(additionalHeaders instanceof Headers) &&
!additionalHeaders.Authorization) || additionalHeaders instanceof Headers && !(additionalHeaders.get('Authorization')))
{
throw new Error(GraphQLAuthError.NO_AUTH_TOKEN);
}

headers = {
Authorization: additionalHeaders.Authorization,
Authorization:
additionalHeaders instanceof Headers
? additionalHeaders.get('Authorization')
: additionalHeaders.Authorization,
};
break;
case 'none':
Expand Down Expand Up @@ -154,7 +163,7 @@ export class InternalGraphQLAPIClass {
| AmplifyClassV6
| ((fn: (amplify: any) => Promise<any>) => Promise<AmplifyClassV6>),
{ query: paramQuery, variables = {}, authMode, authToken }: GraphQLOptions,
additionalHeaders?: { [key: string]: string },
additionalHeaders?: CustomHeaders,
customUserAgentDetails?: CustomUserAgentDetails
): Observable<GraphQLResult<T>> | Promise<GraphQLResult<T>> {
const query =
Expand All @@ -168,11 +177,18 @@ export class InternalGraphQLAPIClass {
const { operation: operationType } =
operationDef as OperationDefinitionNode;

const headers = additionalHeaders || {};
let headers = additionalHeaders || {};

// if an authorization header is set, have the explicit authToken take precedence
if (authToken) {
headers.Authorization = authToken;
if (headers instanceof Headers) {
headers.set('Authorization', authToken);
} else if (typeof headers === 'object') {
headers = {
...headers,
Authorization: authToken,
};
}
}

switch (operationType) {
Expand Down Expand Up @@ -225,7 +241,7 @@ export class InternalGraphQLAPIClass {
private async _graphql<T = any>(
amplify: AmplifyClassV6,
{ query, variables, authMode }: GraphQLOptions,
additionalHeaders = {},
additionalHeaders: CustomHeaders = {},
abortController: AbortController,
customUserAgentDetails?: CustomUserAgentDetails
): Promise<GraphQLResult<T>> {
Expand All @@ -251,14 +267,22 @@ export class InternalGraphQLAPIClass {
* Client or request-specific custom headers that may or may not be
* returned by a function:
*/
let additionalCustomHeaders: Record<string, string> | Headers;

if (typeof additionalHeaders === 'function') {
additionalHeaders = await additionalHeaders();
additionalCustomHeaders = await additionalHeaders();
} else {
additionalCustomHeaders = additionalHeaders;
}

// TODO: Figure what we need to do to remove `!`'s.
const headers = {
...(!customEndpoint &&
(await this._headerBasedAuth(amplify, authMode!, additionalHeaders))),
(await this._headerBasedAuth(
amplify,
authMode!,
additionalCustomHeaders
))),
/**
* Custom endpoint headers.
* If there is both a custom endpoint and custom region present, we get the headers.
Expand All @@ -267,7 +291,11 @@ export class InternalGraphQLAPIClass {
*/
...((customEndpoint &&
(customEndpointRegion
? await this._headerBasedAuth(amplify, authMode!, additionalHeaders)
? await this._headerBasedAuth(
amplify,
authMode!,
additionalCustomHeaders
)
: {})) ||
{}),
// Custom headers included in Amplify configuration options:
Expand Down
3 changes: 2 additions & 1 deletion packages/api-graphql/src/internals/v6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GraphQLOptionsV6,
GraphQLResponseV6,
} from '../types';
import { CustomHeaders } from '@aws-amplify/data-schema-types';

/**
* Invokes graphql operations against a graphql service, providing correct input and
Expand Down Expand Up @@ -98,7 +99,7 @@ export function graphql<
>(
this: V6Client,
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
additionalHeaders?: { [key: string]: string }
additionalHeaders?: CustomHeaders
): GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING> {
/**
* The correctness of these typings depends on correct string branding or overrides.
Expand Down
20 changes: 8 additions & 12 deletions packages/api-graphql/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export interface AWSAppSyncRealTimeProviderOptions {
apiKey?: string;
region?: string;
graphql_headers?: () => {} | (() => Promise<{}>);
additionalHeaders?: { [key: string]: string };
additionalHeaders?: CustomHeaders;
}

export type AWSAppSyncRealTimeProvider = {
Expand Down Expand Up @@ -410,11 +410,7 @@ export type GraphQLMethod = <
TYPED_GQL_STRING extends string = string
>(
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
additionalHeaders?:
| {
[key: string]: string;
}
| undefined
additionalHeaders?: CustomHeaders | undefined
) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;

export type GraphQLMethodSSR = <
Expand All @@ -423,11 +419,7 @@ export type GraphQLMethodSSR = <
>(
contextSpec: AmplifyServer.ContextSpec,
options: GraphQLOptionsV6<FALLBACK_TYPES, TYPED_GQL_STRING>,
additionalHeaders?:
| {
[key: string]: string;
}
| undefined
additionalHeaders?: CustomHeaders | undefined
) => GraphQLResponseV6<FALLBACK_TYPES, TYPED_GQL_STRING>;

/**
Expand All @@ -446,7 +438,11 @@ export type ServerClientGenerationParams = {

export type QueryArgs = Record<string, unknown>;

export type ListArgs = { selectionSet?: string[]; filter?: {} };
export type ListArgs = {
selectionSet?: string[];
filter?: {};
headers?: CustomHeaders;
};

export type AuthModeParams = {
authMode?: GraphQLAuthMode;
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/internals/InternalAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
CustomUserAgentDetails,
} from '@aws-amplify/core/internals/utils';
import { Observable } from 'rxjs';
import { CustomHeaders } from '@aws-amplify/data-schema-types';

/**
* NOTE!
Expand Down Expand Up @@ -66,7 +67,7 @@ export class InternalAPIClass {
*/
graphql<T>(
options: GraphQLOptions,
additionalHeaders?: { [key: string]: string },
additionalHeaders?: CustomHeaders,
customUserAgentDetails?: CustomUserAgentDetails
): T extends GraphQLQuery<T>
? Promise<GraphQLResult<T>>
Expand All @@ -78,7 +79,7 @@ export class InternalAPIClass {
: Promise<GraphQLResult<any>> | Observable<object>;
graphql<T = any>(
options: GraphQLOptions,
additionalHeaders?: { [key: string]: string },
additionalHeaders?: CustomHeaders,
customUserAgentDetails?: CustomUserAgentDetails
): Promise<GraphQLResult<any>> | Observable<object> {
const apiUserAgentDetails: CustomUserAgentDetails = {
Expand Down
17 changes: 12 additions & 5 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f0bbb18

Please sign in to comment.