Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@aws-amplify/data-schema

Package Overview
Dependencies
Maintainers
9
Versions
172
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@aws-amplify/data-schema - npm Package Compare versions

Comparing version 0.0.0-ai-20240906174753 to 0.0.0-asynclambda-20240911175119

21

dist/cjs/CustomOperation.js

@@ -7,2 +7,3 @@ 'use strict';

const Authorization_1 = require("./Authorization");
const Brand_1 = require("./util/Brand");
const queryBrand = 'queryCustomOperation';

@@ -49,2 +50,5 @@ const mutationBrand = 'mutationCustomOperation';

: [handlers];
if (lastHandlerIsAsyncFunction(handlers)) {
data.returnType = eventInvocationResponse;
}
return this;

@@ -117,2 +121,19 @@ },

exports.subscription = subscription;
const eventInvocationResponse = {
data: {
type: 'ref',
link: 'EventInvocationResponse',
valueRequired: false,
array: false,
arrayRequired: false,
mutationOperations: [],
authorization: [],
},
};
function lastHandlerIsAsyncFunction(handlers) {
const lastHandlerBrandSymbol = Array.isArray(handlers)
? handlers[handlers.length - 1][Brand_1.brandSymbol]
: handlers[Brand_1.brandSymbol];
return lastHandlerBrandSymbol === 'asyncFunctionHandler';
}
/**

@@ -119,0 +140,0 @@ * @experimental

21

dist/cjs/Handler.js

@@ -67,2 +67,3 @@ 'use strict';

const functionHandlerBrand = 'functionHandler';
const asyncFunctionHandlerBrand = 'asyncFunctionHandler';
/**

@@ -104,4 +105,22 @@ * Use a function created via `defineFunction` to handle the custom query/mutation/subscription. In your function handler,

function fcn(fn) {
return { [dataSymbol]: fn, ...buildHandler(functionHandlerBrand) };
return {
[dataSymbol]: {
handler: fn,
invocationType: 'RequestResponse',
},
async() {
return _async(this);
},
...buildHandler(functionHandlerBrand),
};
}
function _async(fnHandler) {
return {
[dataSymbol]: {
handler: fnHandler[dataSymbol].handler,
invocationType: 'Event',
},
...buildHandler(asyncFunctionHandlerBrand),
};
}
//#endregion

@@ -108,0 +127,0 @@ exports.handler = {

4

dist/esm/ClientSchema/Core/ClientCustomOperations.d.ts

@@ -1,2 +0,2 @@

import type { CustomOperationParamShape } from '../../CustomOperation';
import type { CustomOperationParamShape, UltimateFunctionHandlerAsyncType } from '../../CustomOperation';
import type { BaseModelField } from '../../ModelField';

@@ -30,3 +30,3 @@ import type { RefType } from '../../RefType';

*/
functionHandler: AppSyncResolverHandler<CustomOpArguments<Op>, LambdaReturnType<CustomOpReturnType<Op, RefBag>>>;
functionHandler: AppSyncResolverHandler<CustomOpArguments<Op>, Op['handlers'] extends UltimateFunctionHandlerAsyncType ? void : LambdaReturnType<CustomOpReturnType<Op, RefBag>>>;
/**

@@ -33,0 +33,0 @@ * The `context.arguments` type for lambda function implementations.

import { SetTypeSubArg } from '@aws-amplify/data-schema-types';
import { Brand } from './util';
import { InternalField, type BaseModelField } from './ModelField';
import { InternalField, ModelField, type BaseModelField } from './ModelField';
import { AllowModifierForCustomOperation, Authorization } from './Authorization';

@@ -8,3 +8,3 @@ import { RefType, InternalRef } from './RefType';

import { CustomType } from './CustomType';
import type { CustomHandler, FunctionHandler, HandlerType as Handler } from './Handler';
import type { AsyncFunctionHandler, CustomHandler, FunctionHandler, HandlerType as Handler } from './Handler';
import { AiModel, InferenceConfiguration } from './ai/ModelType';

@@ -22,3 +22,9 @@ declare const queryBrand = "queryCustomOperation";

type InternalCustomReturnType = InternalRef;
type HandlerInputType = FunctionHandler[] | CustomHandler[] | Handler;
type HandlerInputType = FunctionHandler[] | CustomHandler[] | AsyncFunctionHandler[] | HeterogeneousFunctionHandlerWithLastAsync | HeterogeneousFunctionHandlerType | Handler;
type HeterogeneousFunctionHandlerType = (FunctionHandler | AsyncFunctionHandler)[];
type HeterogeneousFunctionHandlerWithLastAsync = [
...HeterogeneousFunctionHandlerType,
AsyncFunctionHandler
];
export type UltimateFunctionHandlerAsyncType = AsyncFunctionHandler | AsyncFunctionHandler[] | HeterogeneousFunctionHandlerWithLastAsync;
export declare const CustomOperationNames: readonly ["Query", "Mutation", "Subscription", "Generation"];

@@ -54,3 +60,3 @@ type CustomOperationName = (typeof CustomOperationNames)[number];

authorization<AuthRuleType extends Authorization<any, any, any>>(callback: (allow: AllowModifierForCustomOperation) => AuthRuleType | AuthRuleType[]): CustomOperation<SetTypeSubArg<T, 'authorization', AuthRuleType[]>, K | 'authorization', B>;
handler<H extends HandlerInputType>(handlers: H): CustomOperation<T, K | 'handler', B>;
handler<H extends HandlerInputType>(handlers: H): [H] extends [UltimateFunctionHandlerAsyncType] ? CustomOperation<AsyncFunctionCustomOperation<T>, K | 'handler' | 'returns', B> : CustomOperation<T, K | 'handler', B>;
for<Source extends SubscriptionSource>(source: Source | Source[]): CustomOperation<T['typeName'] extends 'Subscription' ? SetTypeSubArg<T, 'returnType', Source extends SubscriptionSource[] ? Source[number] : Source> : T, K | 'for', B>;

@@ -135,2 +141,8 @@ }, K> & Brand<B>;

}, 'returns', typeof subscriptionBrand>;
type AsyncFunctionCustomOperation<T extends CustomOperationParamShape> = SetTypeSubArg<SetTypeSubArg<T, 'returnType', EventInvocationResponseCustomType>, 'handlers', AsyncFunctionHandler>;
type EventInvocationResponseCustomType = CustomType<{
fields: {
success: ModelField<boolean, 'required', undefined>;
};
}>;
export interface GenerationInput {

@@ -137,0 +149,0 @@ aiModel: AiModel;

import type { DefineFunction } from '@aws-amplify/data-schema-types';
import { Brand } from './util';
import type { brandSymbol } from './util/Brand.js';
import { RefType } from './RefType';
export type HandlerType = InlineSqlHandler | SqlReferenceHandler | CustomHandler | FunctionHandler;
export type HandlerType = InlineSqlHandler | SqlReferenceHandler | CustomHandler | FunctionHandler | AsyncFunctionHandler;
declare const dataSymbol: unique symbol;
type AllHandlers = InlineSqlHandler | SqlReferenceHandler | CustomHandler | FunctionHandler;
type AllHandlers = InlineSqlHandler | SqlReferenceHandler | CustomHandler | FunctionHandler | AsyncFunctionHandler;
export declare function getHandlerData<H extends AllHandlers>(handler: H): H[typeof dataSymbol];

@@ -11,3 +11,4 @@ declare const inlineSqlBrand = "inlineSql";

[dataSymbol]: string;
} & Brand<typeof inlineSqlBrand>;
[brandSymbol]: typeof inlineSqlBrand;
};
declare function inlineSql(sql: string): InlineSqlHandler;

@@ -21,3 +22,4 @@ declare const sqlReferenceBrand = "sqlReference";

[dataSymbol]: SqlReferenceHandlerData;
} & Brand<typeof sqlReferenceBrand>;
[brandSymbol]: typeof sqlReferenceBrand;
};
declare function sqlReference(sqlFilePath: string): SqlReferenceHandler;

@@ -45,3 +47,4 @@ type CustomHandlerInput = {

[dataSymbol]: CustomHandlerData;
} & Brand<typeof customHandlerBrand>;
[brandSymbol]: typeof customHandlerBrand;
};
/**

@@ -75,7 +78,52 @@ * Use a custom JavaScript resolver to handle a query, mutation, or subscription.

declare function custom(customHandler: CustomHandlerInput): CustomHandler;
export type FunctionHandlerData = DefineFunction | string;
export type FunctionHandlerInput = DefineFunction | string;
export type FunctionHandlerData = {
handler: FunctionHandlerInput;
invocationType: 'RequestResponse' | 'Event';
};
declare const functionHandlerBrand = "functionHandler";
export type FunctionHandler = {
[dataSymbol]: FunctionHandlerData;
} & Brand<typeof functionHandlerBrand>;
/**
* Use `async()` to have this function handler invoked asynchronously.
* If an `async()` function is only handler or the final handler in a pipeline, the return type of this
* custom query / mutation is `{ success: boolean }`.
* @example
* import {
* type ClientSchema,
* a,
* defineData,
* defineFunction // 1.Import "defineFunction" to create new functions
* } from '@aws-amplify/backend';
*
* // 2. define a function
* const echoHandler = defineFunction({
* entry: './echo-handler/handler.ts'
* })
*
* const schema = a.schema({
* EchoResponse: a.customType({
* content: a.string(),
* executionDuration: a.float()
* }),
*
* echo: a
* .query()
* .arguments({ content: a.string() })
* .returns(a.ref('EchoResponse'))
* .authorization(allow => [allow.publicApiKey()])
* // 3. set the function has the handler
* .handler(a.handler.function(echoHandler).async())
*
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/INSERT_PATH_WHEN_READY}
* @returns A function handler for query / mutation that is asynchronously invoked.
*/
async(): AsyncFunctionHandler;
[brandSymbol]: typeof functionHandlerBrand;
};
declare const asyncFunctionHandlerBrand = "asyncFunctionHandler";
export type AsyncFunctionHandler = {
[dataSymbol]: FunctionHandlerData;
[brandSymbol]: typeof asyncFunctionHandlerBrand;
};
/**

@@ -116,3 +164,3 @@ * Use a function created via `defineFunction` to handle the custom query/mutation/subscription. In your function handler,

*/
declare function fcn(fn: FunctionHandlerData): FunctionHandler;
declare function fcn(fn: FunctionHandlerInput): FunctionHandler;
export declare const handler: {

@@ -119,0 +167,0 @@ inlineSql: typeof inlineSql;

{
"name": "@aws-amplify/data-schema",
"version": "0.0.0-ai-20240906174753",
"version": "0.0.0-asynclambda-20240911175119",
"license": "Apache-2.0",

@@ -5,0 +5,0 @@ "repository": {

@@ -1,3 +0,6 @@

import type { CustomOperationParamShape } from '../../CustomOperation';
import type { BaseModelField } from '../../ModelField';
import type {
CustomOperationParamShape,
UltimateFunctionHandlerAsyncType,
} from '../../CustomOperation';
import type { BaseModelField, ModelFieldType } from '../../ModelField';
import type { RefType } from '../../RefType';

@@ -39,3 +42,9 @@ import type { ResolveFieldRequirements } from '../../MappedTypes/ResolveFieldProperties';

CustomOpArguments<Op>,
LambdaReturnType<CustomOpReturnType<Op, RefBag>>
// If the final handler is an async function, the Schema['fieldname']['functionhandler']
// should have a return type of `void`. This only applies to `functionHandler` and not
// `returnType` because `returnType` determines the type returned by the mutation / query
// in the data client.
Op['handlers'] extends UltimateFunctionHandlerAsyncType
? void
: LambdaReturnType<CustomOpReturnType<Op, RefBag>>
>;

@@ -104,2 +113,22 @@

type _EventInvocationResponseBag = {
EventInvocationResponse: {
__entityType: 'customType';
type: '';
data: {
fields: {
success: {
data: {
fieldType: ModelFieldType.Boolean;
required: true;
array: false;
arrayRequired: false;
};
};
};
type: 'customType';
};
};
};
/**

@@ -106,0 +135,0 @@ * Computes the return type from the `returnType` of a custom operation shape.

import { SetTypeSubArg } from '@aws-amplify/data-schema-types';
import { Brand, brand } from './util';
import { InternalField, type BaseModelField } from './ModelField';
import { InternalField, ModelField, type BaseModelField } from './ModelField';
import {

@@ -13,2 +13,3 @@ AllowModifierForCustomOperation,

import type {
AsyncFunctionHandler,
CustomHandler,

@@ -18,2 +19,3 @@ FunctionHandler,

} from './Handler';
import { brandSymbol } from './util/Brand';
import { AiModel, InferenceConfiguration } from './ai/ModelType';

@@ -33,11 +35,28 @@

type CustomArguments = Record<string, BaseModelField | EnumType>;
type SubscriptionSource = RefType<any, any>;
type InternalSubscriptionSource = InternalRef;
type CustomReturnType = RefType<any> | CustomType<any>;
type InternalCustomArguments = Record<string, InternalField>;
type InternalCustomReturnType = InternalRef;
type HandlerInputType = FunctionHandler[] | CustomHandler[] | Handler;
type HandlerInputType =
| FunctionHandler[]
| CustomHandler[]
| AsyncFunctionHandler[]
| HeterogeneousFunctionHandlerWithLastAsync
| HeterogeneousFunctionHandlerType
| Handler;
type HeterogeneousFunctionHandlerType = (
| FunctionHandler
| AsyncFunctionHandler
)[];
type HeterogeneousFunctionHandlerWithLastAsync = [
...HeterogeneousFunctionHandlerType,
AsyncFunctionHandler,
];
export type UltimateFunctionHandlerAsyncType =
| AsyncFunctionHandler
| AsyncFunctionHandler[]
| HeterogeneousFunctionHandlerWithLastAsync;
export const CustomOperationNames = [

@@ -110,3 +129,10 @@ 'Query',

handlers: H,
): CustomOperation<T, K | 'handler', B>;
): [H] extends [UltimateFunctionHandlerAsyncType]
? CustomOperation<
AsyncFunctionCustomOperation<T>,
K | 'handler' | 'returns',
B
>
: CustomOperation<T, K | 'handler', B>;
for<Source extends SubscriptionSource>(

@@ -190,2 +216,6 @@ source: Source | Source[],

if (lastHandlerIsAsyncFunction(handlers)) {
data.returnType = eventInvocationResponse;
}
return this;

@@ -315,2 +345,35 @@ },

// #region async Lambda function related types
type AsyncFunctionCustomOperation<T extends CustomOperationParamShape> =
SetTypeSubArg<
SetTypeSubArg<T, 'returnType', EventInvocationResponseCustomType>,
'handlers',
AsyncFunctionHandler
>;
type EventInvocationResponseCustomType = CustomType<{
fields: {
success: ModelField<boolean, 'required', undefined>;
};
}>;
const eventInvocationResponse = {
data: {
type: 'ref',
link: 'EventInvocationResponse',
valueRequired: false,
array: false,
arrayRequired: false,
mutationOperations: [],
authorization: [],
},
};
function lastHandlerIsAsyncFunction(handlers: HandlerInputType): boolean {
const lastHandlerBrandSymbol = Array.isArray(handlers)
? handlers[handlers.length - 1][brandSymbol]
: handlers[brandSymbol];
return lastHandlerBrandSymbol === 'asyncFunctionHandler';
}
// #endregion async Lambda function related types
export interface GenerationInput {

@@ -317,0 +380,0 @@ aiModel: AiModel;

import type { DefineFunction } from '@aws-amplify/data-schema-types';
import { Brand, brand } from './util';
import type { brandSymbol } from './util/Brand.js';
import { RefType } from './RefType';

@@ -9,3 +10,4 @@

| CustomHandler
| FunctionHandler;
| FunctionHandler
| AsyncFunctionHandler;

@@ -22,3 +24,4 @@ const dataSymbol = Symbol('Data');

| CustomHandler
| FunctionHandler;
| FunctionHandler
| AsyncFunctionHandler;

@@ -35,5 +38,6 @@ export function getHandlerData<H extends AllHandlers>(

export type InlineSqlHandler = { [dataSymbol]: string } & Brand<
typeof inlineSqlBrand
>;
export type InlineSqlHandler = {
[dataSymbol]: string;
[brandSymbol]: typeof inlineSqlBrand;
};

@@ -57,3 +61,4 @@ function inlineSql(sql: string): InlineSqlHandler {

[dataSymbol]: SqlReferenceHandlerData;
} & Brand<typeof sqlReferenceBrand>;
[brandSymbol]: typeof sqlReferenceBrand;
};

@@ -95,8 +100,9 @@ function sqlReference(sqlFilePath: string): SqlReferenceHandler {

export type CustomHandler = { [dataSymbol]: CustomHandlerData } & Brand<
typeof customHandlerBrand
>;
export type CustomHandler = {
[dataSymbol]: CustomHandlerData;
[brandSymbol]: typeof customHandlerBrand;
};
/**
* Use a custom JavaScript resolver to handle a query, mutation, or subscription.
* Use a custom JavaScript resolver to handle a query, mutation, or subscription.
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/#step-2---configure-custom-business-logic-handler-code}

@@ -115,3 +121,3 @@ * @param customHandler `{ entry: "path-to-javascript-resolver-file.js", dataSource: "Data Source name added via "backend.data.add*DataSoruce(...)"}`

* ]),
*
*
* likePost: a

@@ -140,5 +146,8 @@ * .mutation()

//#region handler.function
export type FunctionHandlerInput = DefineFunction | string;
export type FunctionHandlerData = {
handler: FunctionHandlerInput;
invocationType: 'RequestResponse' | 'Event';
};
export type FunctionHandlerData = DefineFunction | string;
const functionHandlerBrand = 'functionHandler';

@@ -148,6 +157,49 @@

[dataSymbol]: FunctionHandlerData;
} & Brand<typeof functionHandlerBrand>;
/**
* Use `async()` to have this function handler invoked asynchronously.
* If an `async()` function is only handler or the final handler in a pipeline, the return type of this
* custom query / mutation is `{ success: boolean }`.
* @example
* import {
* type ClientSchema,
* a,
* defineData,
* defineFunction // 1.Import "defineFunction" to create new functions
* } from '@aws-amplify/backend';
*
* // 2. define a function
* const echoHandler = defineFunction({
* entry: './echo-handler/handler.ts'
* })
*
* const schema = a.schema({
* EchoResponse: a.customType({
* content: a.string(),
* executionDuration: a.float()
* }),
*
* echo: a
* .query()
* .arguments({ content: a.string() })
* .returns(a.ref('EchoResponse'))
* .authorization(allow => [allow.publicApiKey()])
* // 3. set the function has the handler
* .handler(a.handler.function(echoHandler).async())
*
* @see {@link https://docs.amplify.aws/react/build-a-backend/data/INSERT_PATH_WHEN_READY}
* @returns A function handler for query / mutation that is asynchronously invoked.
*/
async(): AsyncFunctionHandler;
[brandSymbol]: typeof functionHandlerBrand;
};
const asyncFunctionHandlerBrand = 'asyncFunctionHandler';
export type AsyncFunctionHandler = {
[dataSymbol]: FunctionHandlerData;
[brandSymbol]: typeof asyncFunctionHandlerBrand;
};
/**
* Use a function created via `defineFunction` to handle the custom query/mutation/subscription. In your function handler,
* Use a function created via `defineFunction` to handle the custom query/mutation/subscription. In your function handler,
* you can use the `Schema["YOUR_QUERY_OR_MUTATION_NAME"]["functionHandler"]` utility type to type the handler function.

@@ -161,3 +213,3 @@ * @example

* } from '@aws-amplify/backend';
*
*
* // 2. define a function

@@ -167,3 +219,3 @@ * const echoHandler = defineFunction({

* })
*
*
* const schema = a.schema({

@@ -174,3 +226,3 @@ * EchoResponse: a.customType({

* }),
*
*
* echo: a

@@ -185,10 +237,29 @@ * .query()

* @see {@link https://docs.amplify.aws/react/build-a-backend/data/custom-business-logic/}
* @param fn A function created via `defineFunction`. Alternatively, you can pass in a "string" of the function name and pass
* @param fn A function created via `defineFunction`. Alternatively, you can pass in a "string" of the function name and pass
* in a corresponding value into the `functionMap` property of defineData.
* @returns A handler for the query / mutation / subscription
*/
function fcn(fn: FunctionHandlerData): FunctionHandler {
return { [dataSymbol]: fn, ...buildHandler(functionHandlerBrand) };
function fcn(fn: FunctionHandlerInput): FunctionHandler {
return {
[dataSymbol]: {
handler: fn,
invocationType: 'RequestResponse',
},
async() {
return _async(this);
},
...buildHandler(functionHandlerBrand),
};
}
function _async(fnHandler: FunctionHandler): AsyncFunctionHandler {
return {
[dataSymbol]: {
handler: fnHandler[dataSymbol].handler,
invocationType: 'Event',
},
...buildHandler(asyncFunctionHandlerBrand),
};
}
//#endregion

@@ -195,0 +266,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc