-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: initialization #6
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I will try testing it out for real soon, but code itself looks good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tried it out and it worked.
I think there are some differences between this and the defined tasks for things like error handling, but those can be resolved later. Merging this will let us get started building the actual sdk functionality 🥳
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need "type": "module" in the package JSON?
I just tried to use it from a simple test app as:
import { createStrapiSDK } from "@strapi/sdk-js";
const strapi = createStrapiSDK({
baseURL: "http://localhost:1337",
});
async function testStrapi() {
try {
// Example: Fetch all entries of a content type
const response = await strapi.fetch("/api/articles");
const articles = await response.json();
console.log("Articles:", articles);
} catch (error) {
console.error("Error interacting with Strapi:", error);
}
}
testStrapi();
And got the error:
file:///Users/jamie/scr/strapi/code/strapi-sdk-test/test.js:1
import { createStrapiSDK } from "@strapi/sdk-js";
^^^^^^^^^^^^^^^
SyntaxError: Named export 'createStrapiSDK' not found. The requested module '@strapi/sdk-js' is a CommonJS module, which may not support all module.exports as named exports.
...
What does it do?
tl;dr Initialize the Strapi SDK project
Implements the tech design originating from the internal Strapi SDK Request For Comment.
When packed and distributed, the package exposes a single factory used to create a new Strapi SDK instance.
Usage
The factory takes a single parameter: the SDK configuration.
Where
baseURL
is used to prepend all future request URLs.auth
configuration is used to authenticate future HTTP requests using a specified auth strategy.TypeScript/ESM
JavaScript/CommonJS
Features
The initialization step includes the following features (whether as a contributor or end user)
Contributor
End-User
sdk.fetch()
methodWhy is it needed?
To make the life of devs using Strapi easier.
How to test it?
The project has a 100% code coverage and is configured to accept a minimum of 95% coverage on all tokens.
Yup, 95%.
Life's good
To run the tests, use
pnpm test
To run the tests with coverage, use
Scripts
build
: Executes Rollup withrollup.config.mjs
. The option--failAfterWarnings
ensures the process exits with an error code if there are any warnings during the build process.build:clean
: First deletes thedist
directory, then similar tobuild
, run Rollup with the same configuration and settings.clean
: Uses Rimraf to recursively delete thedist
directory, cleaning up build artifacts.lint
: Runs ESLint across the entire codebase to identify style and programming issues.lint:fix
: Executes ESLint with the--fix
option to automatically correct certain linting issues.prepare
: Rely on husky to run pre-commit hooks (lint, tests, build, etc...)prettier:check
: Runs Prettier to check if code formatting matches the project’s style rules, without making changes.prettier:write
: Executes Prettier to reformat code according to the specified style rules.test
: Runs Jest in verbose mode to execute the test suites.test:cov
: Runs Jest in verbose mode with a coverage report, offering insights into how much of the codebase is covered by tests.ts:check
: Uses the TS Compiler to check for type errors in the project as specified bytsconfig.build.json
, without outputting compiled files (--noEmit
).watch
: Runs Rollup in watch mode, which re-builds the project on file changes, again using--failAfterWarnings
to stop the process on warnings.prepack
: Executes a shell script located at./scripts/pre-pack.sh
. Used to prepare the package for publishing, performing tasks such as linting, testing or compiling files.Overall Simplified Architecture
Although it exposes a simple factory function to the public API, the SDK package is designed in an object-oriented way.
The entry point and main class is the
StrapiSDK
.It uses an
HttpClient
dependency to perform requests based on the user configuration.The
HttpClient
manages the authentication of its requests using anAuthManager
.The
AuthManager
is responsible for managing its internal state (auth provider, authentication state, etc...).AuthProviderFactory
to create instances of necessary auth providersApiTokenAuthProvider
UsersPermissionsAuthProvider
Architecture Diagram
Note: only the public API of each class is shown here to avoid verbosity and keep it simple
Damn, you really read all that? Congrats I guess?
???