-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk-javascript.html
More file actions
414 lines (356 loc) · 15.5 KB
/
sdk-javascript.html
File metadata and controls
414 lines (356 loc) · 15.5 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-5C3LL34WWW"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-5C3LL34WWW');
</script>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript SDK - Local Web Services</title>
<meta name="description" content="local-web-services-javascript-sdk: Jest AWS service testing for Node.js. Pre-configured AWS SDK v3 clients and automatic endpoint redirection via environment variables.">
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/themes/prism-tomorrow.min.css">
</head>
<body>
<nav class="nav">
<div class="container nav-inner">
<a href="index.html" class="nav-logo">Local Web Services</a>
<ul class="nav-links">
<li><a href="getting-started.html">Get Started</a></li>
<li><a href="sdks.html" class="nav-active">SDKs</a></li>
<li><a href="services.html">Cloud Emulation</a></li>
<li><a href="faking.html">Faking Services</a></li>
<li><a href="chaos.html">Chaos Engineering</a></li>
<li><a href="cli.html">CLI</a></li>
<li><a href="https://github.com/local-web-services/local-web-services" class="nav-github">GitHub</a></li>
</ul>
</div>
</nav>
<header class="page-header">
<div class="container">
<h1>JavaScript SDK</h1>
<p class="section-lead">AWS service testing for Node.js. When a session starts, <code>AWS_ENDPOINT_URL_*</code> environment variables are set automatically — every AWS SDK v3 client in your application talks to local services without any code changes.</p>
<div style="display: flex; gap: 16px; justify-content: center; margin-top: 24px; flex-wrap: wrap;">
<a href="https://www.npmjs.com/package/local-web-services-javascript-sdk" class="btn btn-primary">npm</a>
<a href="https://github.com/local-web-services/local-web-services/tree/main/lang/javascript/sdk" class="btn btn-secondary">GitHub</a>
<a href="https://github.com/local-web-services/local-web-services/tree/main/lang/javascript/example" class="btn btn-secondary">Example Project</a>
<a href="sdks.html" class="btn btn-secondary">All SDKs</a>
</div>
</div>
</header>
<!-- Quick Start -->
<section class="section">
<div class="container">
<h2>Quick Start</h2>
<p class="section-lead">Install, create a session in <code>beforeAll</code>, and write a test. Local services start as a subprocess — your application's AWS SDK clients redirect to them automatically.</p>
<div class="steps" style="max-width: 900px;">
<div class="step">
<div class="step-number">1</div>
<div class="step-content">
<h3>Install</h3>
<div class="code-block">
<code>npm install local-web-services-javascript-sdk
# also requires local-web-services (Python) for ldk dev
pip install local-web-services</code>
</div>
</div>
</div>
<div class="step">
<div class="step-number">2</div>
<div class="step-content">
<h3>Configure Jest</h3>
<div class="code-block">
<code>// jest.config.js
module.exports = {
testEnvironment: 'node',
testTimeout: 60000, // allow time for ldk dev to start
};</code>
</div>
</div>
</div>
<div class="step">
<div class="step-number">3</div>
<div class="step-content">
<h3>Write your test</h3>
<div class="code-block">
<code>// orders.test.js
const { LwsSession } = require('local-web-services-javascript-sdk');
let session;
beforeAll(async () => {
session = await LwsSession.create({
tables: [{ name: 'Orders', partitionKey: 'id' }],
queues: ['OrderQueue'],
});
});
afterAll(async () => {
await session.close();
});
beforeEach(async () => {
await session.reset(); // wipe state between tests
});
test('creates an order', async () => {
// Call your real application code — it uses standard AWS SDK clients
const { createOrder } = require('./orders');
await createOrder({ id: '42', status: 'pending' });
// Assert using the DynamoDB helper
const table = session.dynamodb('Orders');
const item = await table.assertItemExists({ id: { S: '42' } });
expect(item.status.S).toBe('pending');
});
test('order sends a queue message', async () => {
const { createOrder } = require('./orders');
await createOrder({ id: '99', status: 'pending' });
const queue = session.sqs('OrderQueue');
await queue.assertMessageCount(1);
});</code>
</div>
</div>
</div>
<div class="step">
<div class="step-number">4</div>
<div class="step-content">
<h3>Run your tests</h3>
<div class="code-block">
<code>npx jest</code>
</div>
<p style="margin-top: 12px; color: var(--color-text-muted);">The session starts <code>ldk dev</code> once in <code>beforeAll</code>, then <code>reset()</code> wipes state before each test.</p>
</div>
</div>
</div>
</div>
</section>
<!-- Drop-in replacement -->
<section class="section section-alt">
<div class="container">
<h2>Drop-in Replacement — Zero Code Changes</h2>
<p class="section-lead">When a session starts, <code>AWS_ENDPOINT_URL_*</code> environment variables are set for every supported service. Any <code>new DynamoDBClient({})</code> your application creates will automatically use local services — no client reconfiguration, no dependency injection.</p>
<div class="comparison comparison--code">
<div class="comparison-col">
<h3>Your application code</h3>
<div class="code-block" style="margin-top: 16px;">
<code>// orders.js — unchanged
const { DynamoDBClient, PutItemCommand } = require('@aws-sdk/client-dynamodb');
const { SQSClient, SendMessageCommand } = require('@aws-sdk/client-sqs');
// Standard clients — no endpoint config
const dynamo = new DynamoDBClient({});
const sqs = new SQSClient({});
async function createOrder(order) {
await dynamo.send(new PutItemCommand({
TableName: 'Orders',
Item: {
id: { S: order.id },
status: { S: order.status },
},
}));
await sqs.send(new SendMessageCommand({
QueueUrl: process.env.ORDER_QUEUE_URL,
MessageBody: JSON.stringify(order),
}));
}
module.exports = { createOrder };</code>
</div>
</div>
<div class="comparison-col">
<h3>Your test</h3>
<div class="code-block" style="margin-top: 16px;">
<code>// orders.test.js
const { LwsSession } = require('local-web-services-javascript-sdk');
let session;
beforeAll(async () => {
session = await LwsSession.create({
tables: [{ name: 'Orders', partitionKey: 'id' }],
queues: ['OrderQueue'],
});
// AWS_ENDPOINT_URL_DYNAMODB and AWS_ENDPOINT_URL_SQS
// are now set — the clients in orders.js pick them
// up automatically on their next call.
});
test('creates an order', async () => {
const { createOrder } = require('./orders');
await createOrder({ id: '42', status: 'pending' });
await session.dynamodb('Orders')
.assertItemExists({ id: { S: '42' } });
await session.sqs('OrderQueue')
.assertMessageCount(1);
});</code>
</div>
</div>
</div>
<div class="feature-grid" style="margin-top: 48px;">
<div class="feature-card">
<div class="feature-icon">🌍</div>
<h3>Env Var Injection</h3>
<p>Sets <code>AWS_ENDPOINT_URL_DYNAMODB</code>, <code>AWS_ENDPOINT_URL_SQS</code>, <code>AWS_ENDPOINT_URL_S3</code>, and more. Restored to original values when <code>session.close()</code> is called.</p>
</div>
<div class="feature-card">
<div class="feature-icon">∅</div>
<h3>No Refactoring</h3>
<p>Application code is identical to production. <code>new DynamoDBClient({})</code> without any endpoint override just works — the AWS SDK v3 reads the env var automatically.</p>
</div>
<div class="feature-card">
<div class="feature-icon">🔓</div>
<h3>No Credentials</h3>
<p>Local services accept any credentials. No AWS account, no IAM setup, no <code>~/.aws/credentials</code> needed in your test environment.</p>
</div>
</div>
</div>
</section>
<!-- LwsSession API -->
<section class="section">
<div class="container">
<h2>LwsSession</h2>
<p class="section-lead">All factory methods are async — they start <code>ldk dev</code> and wait until services are ready before resolving.</p>
<h3 class="section-subheading section-subheading--primary">Factory methods</h3>
<div class="code-block config-example">
<code>const { LwsSession } = require('local-web-services-javascript-sdk');
// Explicit resource declaration
const session = await LwsSession.create({
tables: [
{ name: 'Orders', partitionKey: 'id' },
{ name: 'Products', partitionKey: 'sku', sortKey: 'version' },
],
queues: ['OrderQueue', 'DeadLetterQueue'],
buckets: ['ReceiptsBucket'],
});
// Auto-discover from CDK cloud assembly (cdk.out/)
const session = await LwsSession.fromCdk('../my-cdk-project');
// Auto-discover from Terraform .tf files
const session = await LwsSession.fromHcl('../my-terraform-project');
// Always close after your tests
await session.close();</code>
</div>
<h3 class="section-subheading section-subheading--primary">Client factory and utilities</h3>
<div class="code-block config-example">
<code>const dynamo = session.client('dynamodb');
const sqs = session.client('sqs');
const s3 = session.client('s3');
const sns = session.client('sns');
const ssm = session.client('ssm');
const secrets = session.client('secretsmanager');
const sfn = session.client('stepfunctions');
const queueUrl = session.queueUrl('OrderQueue'); // local SQS URL
const port = session.portFor('dynamodb'); // port number
await session.reset(); // clear all tables, queues, buckets</code>
</div>
</div>
</section>
<!-- Resource Helpers -->
<section class="section section-alt">
<div class="container">
<h2>Resource Helpers</h2>
<p class="section-lead">Async helpers with built-in assertion methods. Skip the boilerplate.</p>
<h3 class="section-subheading section-subheading--primary">DynamoDB</h3>
<div class="code-block config-example">
<code>const table = session.dynamodb('Orders');
await table.put({ id: { S: '1' }, status: { S: 'pending' } });
const item = await table.get({ id: { S: '1' } });
await table.delete({ id: { S: '1' } });
const items = await table.scan();
await table.assertItemExists({ id: { S: '1' } }); // returns item
await table.assertItemCount(5);</code>
</div>
<h3 class="section-subheading section-subheading--primary">SQS</h3>
<div class="code-block config-example">
<code>const queue = session.sqs('OrderQueue');
await queue.send({ orderId: '42', total: 99.99 }); // object → JSON
await queue.send('plain text message');
const messages = await queue.receive({ maxMessages: 10 });
await queue.purge();
await queue.assertMessageCount(3);
console.log(queue.url); // http://localhost:.../OrderQueue</code>
</div>
<h3 class="section-subheading section-subheading--primary">S3</h3>
<div class="code-block config-example">
<code>const bucket = session.s3('ReceiptsBucket');
await bucket.put('receipts/001.pdf', Buffer.from('PDF bytes'));
await bucket.put('config.json', JSON.stringify({ key: 'value' }));
const data = await bucket.get('receipts/001.pdf'); // Buffer
const text = await bucket.getText('config.json'); // string
const keys = await bucket.listKeys({ prefix: 'receipts/' });
await bucket.delete('config.json');
await bucket.assertObjectExists('receipts/001.pdf');
await bucket.assertObjectCount({ expected: 5, prefix: 'receipts/' });</code>
</div>
</div>
</section>
<!-- Faking -->
<section class="section">
<div class="container">
<h2>Operation Faking</h2>
<p class="section-lead">Override any AWS operation response with a fluent builder. No changes to application code needed.</p>
<div class="code-block config-example">
<code>// Inject throttling
await session.fake('dynamodb').operation('PutItem').respond({
status: 400,
body: { __type: 'ProvisionedThroughputExceededException' },
});
// Add artificial latency
await session.fake('sqs').operation('SendMessage').respond({
status: 200,
delayMs: 500,
});
// Clear all fakes for a service
await session.fake('dynamodb').clear();</code>
</div>
</div>
</section>
<!-- Chaos -->
<section class="section section-alt">
<div class="container">
<h2>Chaos Engineering</h2>
<p class="section-lead">Inject failure conditions programmatically to verify your application handles them gracefully.</p>
<div class="code-block config-example">
<code>await session.chaos('dynamodb').errorRate(0.3).latency({ minMs: 50, maxMs: 200 }).apply();
await session.chaos('sqs').connectionResetRate(0.1).timeoutRate(0.05).apply();
await session.chaos('dynamodb').clear();</code>
</div>
</div>
</section>
<!-- IAM -->
<section class="section">
<div class="container">
<h2>IAM Authorization</h2>
<p class="section-lead">Switch IAM modes and define named identities with inline policies to test authorization behaviour.</p>
<div class="code-block config-example">
<code>await session.iam.mode('enforce').apply();
await session.iam.mode('audit').apply();
await session.iam.mode('disabled').apply(); // default
await session.iam.defaultIdentity('service-account').apply();
await session.iam
.identity('readonly')
.allow(['dynamodb:GetItem', 'dynamodb:Scan'])
.apply();</code>
</div>
</div>
</section>
<footer class="footer">
<div class="container">
<div class="footer-inner">
<div class="footer-brand">
<a href="index.html" class="nav-logo">Local Web Services</a>
<p>The fastest feedback loop for cloud-native development, for humans and AI alike</p>
</div>
<div class="footer-links">
<a href="https://github.com/local-web-services/local-web-services">GitHub</a>
<a href="https://github.com/local-web-services/local-web-services/tree/main/lang/python/sdk">Python SDK</a>
<a href="https://pypi.org/project/local-web-services-python-sdk/">PyPI</a>
<a href="https://www.npmjs.com/package/local-web-services-javascript-sdk">npm</a>
<a href="https://github.com/local-web-services/local-web-services/issues">Issues</a>
</div>
</div>
<p class="footer-copy">Open source under the MIT License.</p>
</div>
</footer>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-core.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-clike.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-javascript.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-bash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/1.29.0/components/prism-yaml.min.js"></script>
<script src="lws.js"></script>
</body>
</html>