Skip to content

Commit 45fe0d4

Browse files
authored
✨ feat: support state store (#189)
* 🌈 style: update import style Signed-off-by: yi-ge-dian <[email protected]> * ✨ feat: support the state store Signed-off-by: yi-ge-dian <[email protected]> * Revert "🌈 style: update import style" This reverts commit edfa1f9. Signed-off-by: yi-ge-dian <[email protected]> * 🦄 refactor: refactor the code for state store Signed-off-by: yi-ge-dian <[email protected]> * 📃 docs: generate docs Signed-off-by: yi-ge-dian <[email protected]> * 🦄 refactor: use lodash to make code more elegant Signed-off-by: yi-ge-dian <[email protected]> * 🌈 cosm: remove console.log Signed-off-by: yi-ge-dian <[email protected]> --------- Signed-off-by: yi-ge-dian <[email protected]>
1 parent 41b5e2c commit 45fe0d4

File tree

11 files changed

+799
-8
lines changed

11 files changed

+799
-8
lines changed

docs/generated/api.json

+140
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,27 @@
577577
},
578578
"releaseTag": "Public",
579579
"name": "PubSub"
580+
},
581+
{
582+
"kind": "EnumMember",
583+
"canonicalReference": "@openfunction/functions-framework!ComponentType.State:member",
584+
"docComment": "/**\n * The state type\n */\n",
585+
"excerptTokens": [
586+
{
587+
"kind": "Content",
588+
"text": "State = "
589+
},
590+
{
591+
"kind": "Content",
592+
"text": "\"state\""
593+
}
594+
],
595+
"initializerTokenRange": {
596+
"startIndex": 1,
597+
"endIndex": 2
598+
},
599+
"releaseTag": "Public",
600+
"name": "State"
580601
}
581602
]
582603
},
@@ -826,6 +847,54 @@
826847
],
827848
"isOptional": false,
828849
"name": "IsPubSubComponent"
850+
},
851+
{
852+
"kind": "Method",
853+
"canonicalReference": "@openfunction/functions-framework!ContextUtils.IsStateComponent:member(1)",
854+
"docComment": "/**\n * Checks if the component is a state component.\n *\n * @param component - The component to check.\n *\n * @returns A boolean value.\n */\n",
855+
"excerptTokens": [
856+
{
857+
"kind": "Content",
858+
"text": "static IsStateComponent(component: "
859+
},
860+
{
861+
"kind": "Reference",
862+
"text": "OpenFunctionComponent",
863+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionComponent:interface"
864+
},
865+
{
866+
"kind": "Content",
867+
"text": "): "
868+
},
869+
{
870+
"kind": "Content",
871+
"text": "boolean"
872+
},
873+
{
874+
"kind": "Content",
875+
"text": ";"
876+
}
877+
],
878+
"isStatic": true,
879+
"returnTypeTokenRange": {
880+
"startIndex": 3,
881+
"endIndex": 4
882+
},
883+
"releaseTag": "Public",
884+
"isProtected": false,
885+
"overloadIndex": 1,
886+
"parameters": [
887+
{
888+
"parameterName": "component",
889+
"parameterTypeTokenRange": {
890+
"startIndex": 1,
891+
"endIndex": 2
892+
},
893+
"isOptional": false
894+
}
895+
],
896+
"isOptional": false,
897+
"name": "IsStateComponent"
829898
}
830899
],
831900
"implementsTokenRanges": []
@@ -2051,6 +2120,47 @@
20512120
"endIndex": 12
20522121
}
20532122
},
2123+
{
2124+
"kind": "PropertySignature",
2125+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionContext#states:member",
2126+
"docComment": "/**\n * Optional state store config;\n */\n",
2127+
"excerptTokens": [
2128+
{
2129+
"kind": "Content",
2130+
"text": "states?: "
2131+
},
2132+
{
2133+
"kind": "Reference",
2134+
"text": "Record",
2135+
"canonicalReference": "!Record:type"
2136+
},
2137+
{
2138+
"kind": "Content",
2139+
"text": "<string, "
2140+
},
2141+
{
2142+
"kind": "Reference",
2143+
"text": "OpenFunctionComponent",
2144+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionComponent:interface"
2145+
},
2146+
{
2147+
"kind": "Content",
2148+
"text": ">"
2149+
},
2150+
{
2151+
"kind": "Content",
2152+
"text": ";"
2153+
}
2154+
],
2155+
"isReadonly": false,
2156+
"isOptional": true,
2157+
"releaseTag": "Public",
2158+
"name": "states",
2159+
"propertyTypeTokenRange": {
2160+
"startIndex": 1,
2161+
"endIndex": 5
2162+
}
2163+
},
20542164
{
20552165
"kind": "PropertySignature",
20562166
"canonicalReference": "@openfunction/functions-framework!OpenFunctionContext#version:member",
@@ -2635,6 +2745,36 @@
26352745
"isStatic": false,
26362746
"isProtected": false
26372747
},
2748+
{
2749+
"kind": "Property",
2750+
"canonicalReference": "@openfunction/functions-framework!OpenFunctionRuntime#state:member",
2751+
"docComment": "/**\n * The promise that handle data by state store.\n */\n",
2752+
"excerptTokens": [
2753+
{
2754+
"kind": "Content",
2755+
"text": "abstract get state(): "
2756+
},
2757+
{
2758+
"kind": "Reference",
2759+
"text": "StateOperations",
2760+
"canonicalReference": "@openfunction/functions-framework!~StateOperations:interface"
2761+
},
2762+
{
2763+
"kind": "Content",
2764+
"text": ";"
2765+
}
2766+
],
2767+
"isReadonly": true,
2768+
"isOptional": false,
2769+
"releaseTag": "Public",
2770+
"name": "state",
2771+
"propertyTypeTokenRange": {
2772+
"startIndex": 1,
2773+
"endIndex": 2
2774+
},
2775+
"isStatic": false,
2776+
"isProtected": false
2777+
},
26382778
{
26392779
"kind": "Property",
26402780
"canonicalReference": "@openfunction/functions-framework!OpenFunctionRuntime#trigger:member",

docs/generated/api.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
import { CloudEventV1 as CloudEvent } from 'cloudevents';
1111
import { IndexedFieldWithPossiblyUndefined } from 'lodash';
12+
import { KeyValueType } from '@dapr/dapr/types/KeyValue.type';
1213
import { ParamsDictionary } from 'express-serve-static-core';
1314
import { ParsedQs } from 'qs';
1415
import { Request as Request_3 } from 'express';
1516
import { Response as Response_2 } from 'express';
17+
import { StateQueryResponseType } from '@dapr/dapr/types/state/StateQueryResponse.type';
1618

1719
export { CloudEvent }
1820

@@ -44,7 +46,8 @@ export interface CloudFunctionsContext {
4446
// @public
4547
export enum ComponentType {
4648
Binding = "bindings",
47-
PubSub = "pubsub"
49+
PubSub = "pubsub",
50+
State = "state"
4851
}
4952

5053
// @public
@@ -56,6 +59,7 @@ export class ContextUtils {
5659
static IsBindingComponent(component: OpenFunctionComponent): boolean;
5760
static IsKnativeRuntime(context: OpenFunctionContext): boolean;
5861
static IsPubSubComponent(component: OpenFunctionComponent): boolean;
62+
static IsStateComponent(component: OpenFunctionComponent): boolean;
5963
}
6064

6165
// @public
@@ -136,6 +140,7 @@ export interface OpenFunctionContext {
136140
postPlugins?: string[];
137141
prePlugins?: string[];
138142
runtime: `${RuntimeType}` | `${Capitalize<RuntimeType>}` | `${Uppercase<RuntimeType>}`;
143+
states?: Record<string, OpenFunctionComponent>;
139144
version: string;
140145
}
141146

@@ -156,6 +161,8 @@ export abstract class OpenFunctionRuntime {
156161
HTTP: string;
157162
GRPC: string;
158163
};
164+
// Warning: (ae-forgotten-export) The symbol "StateOperations" needs to be exported by the entry point index.d.ts
165+
abstract get state(): StateOperations;
159166
// Warning: (ae-forgotten-export) The symbol "OpenFunctionTrigger" needs to be exported by the entry point index.d.ts
160167
protected trigger?: OpenFunctionTrigger;
161168
static WrapUserFunction(userFunction: OpenFunction, context: OpenFunctionContext | OpenFunctionRuntime): (data: any) => Promise<void>;

src/openfunction/context.ts

+16
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export interface OpenFunctionContext {
3030
* Optional output binding object.
3131
*/
3232
outputs?: Record<string, OpenFunctionComponent>;
33+
/**
34+
* Optional state store config;
35+
*/
36+
states?: Record<string, OpenFunctionComponent>;
3337
/**
3438
* Optional plugins to be executed before user function.
3539
*/
@@ -99,6 +103,10 @@ export enum ComponentType {
99103
* The pubsub type.
100104
*/
101105
PubSub = 'pubsub',
106+
/**
107+
* The state type
108+
*/
109+
State = 'state',
102110
}
103111

104112
/**
@@ -139,6 +147,14 @@ export class ContextUtils {
139147
static IsPubSubComponent(component: OpenFunctionComponent): boolean {
140148
return component?.componentType.split('.')[0] === ComponentType.PubSub;
141149
}
150+
/**
151+
* Checks if the component is a state component.
152+
* @param component - The component to check.
153+
* @returns A boolean value.
154+
*/
155+
static IsStateComponent(component: OpenFunctionComponent): boolean {
156+
return component?.componentType.split('.')[0] === ComponentType.State;
157+
}
142158
}
143159

144160
/**

0 commit comments

Comments
 (0)