-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfunctions.ts
More file actions
158 lines (121 loc) · 3.22 KB
/
functions.ts
File metadata and controls
158 lines (121 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import * as sdk from "@hasura/ndc-lambda-sdk";
// ── Scalar types ──
/** @readonly */
export function hello(name?: string): string {
return `hello ${name ?? "world"}`;
}
/** @readonly */
export function add(a: number, b: number): number {
return a + b;
}
/** @readonly */
export function isTrue(value: boolean): boolean {
return value === true;
}
/** @readonly */
export function echoBigInt(value: bigint): bigint {
return value;
}
// ── Nullable / optional args ──
/** @readonly */
export function greetNullable(name: string | null): string {
return `hello ${name ?? "anonymous"}`;
}
/** @readonly */
export function greetOptional(name?: string): string {
return `hello ${name ?? "default"}`;
}
// ── Object types ──
export type Coordinates = {
lat: number;
lng: number;
};
export type Place = {
name: string;
location: Coordinates;
};
/** @readonly */
export function getDistance(from: Coordinates, to: Coordinates): number {
return Math.sqrt(
Math.pow(to.lat - from.lat, 2) + Math.pow(to.lng - from.lng, 2)
);
}
/** @readonly */
export function describePlace(place: Place): string {
return `${place.name} is at (${place.location.lat}, ${place.location.lng})`;
}
// ── Array types ──
/** @readonly */
export function sumArray(numbers: number[]): number {
return numbers.reduce((acc, n) => acc + n, 0);
}
/** @readonly */
export function reverseStrings(items: string[]): string[] {
return [...items].reverse();
}
// ── Nested return types ──
type Address = {
street: string;
city: string;
};
type PersonWithAddress = {
name: string;
age: number;
address: Address;
};
/** @readonly */
export function getPersonWithAddress(name: string, age: number, street: string, city: string): PersonWithAddress {
return { name, age, address: { street, city } };
}
// ── Async functions ──
/** @readonly */
export async function asyncGreet(name: string): Promise<string> {
return `async hello ${name}`;
}
/** @readonly */
export async function asyncGetPlace(name: string, lat: number, lng: number): Promise<Place> {
return { name, location: { lat, lng } };
}
// ── Procedures (mutations) ──
let counter = 0;
export function incrementCounter(): number {
counter += 1;
return counter;
}
export function resetCounter(): number {
counter = 0;
return counter;
}
type User = {
id: number;
name: string;
email: string;
};
let nextUserId = 1;
export function createUser(name: string, email: string): User {
return { id: nextUserId++, name, email };
}
type Item = {
id: string;
title: string;
};
export async function asyncCreateItem(title: string): Promise<Item> {
return { id: "item-1", title };
}
// ── SDK error functions ──
/** @readonly */
export function throwForbidden(): string {
throw new sdk.Forbidden("access denied", { reason: "no permission" });
}
/** @readonly */
export function throwConflict(): string {
throw new sdk.Conflict("resource conflict", { resource: "item" });
}
/** @readonly */
export function throwUnprocessable(): string {
throw new sdk.UnprocessableContent("invalid input", { field: "name" });
}
/** @readonly */
export function throwInternalError(): string {
throw new Error("something went wrong");
}