<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: admond</title>
    <description>The latest articles on DEV Community by admond (@admondtamang).</description>
    <link>https://dev.to/admondtamang</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F518687%2F47f7b786-edf2-4ceb-af52-dff29ab6068e.png</url>
      <title>DEV Community: admond</title>
      <link>https://dev.to/admondtamang</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/admondtamang"/>
    <language>en</language>
    <item>
      <title>Buid your own go package</title>
      <dc:creator>admond</dc:creator>
      <pubDate>Fri, 13 Sep 2024 09:02:10 +0000</pubDate>
      <link>https://dev.to/admondtamang/buid-your-own-go-package-55lo</link>
      <guid>https://dev.to/admondtamang/buid-your-own-go-package-55lo</guid>
      <description>&lt;h2&gt;
  
  
  Local Development of Go Packages: Fork, Modify, and Replace
&lt;/h2&gt;

&lt;p&gt;To locally develop a Go package, follow these steps:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Fork and Clone the Repository
&lt;/h3&gt;

&lt;p&gt;Fork the repository you want to modify and clone it to your preferred directory. You can clone the repository to any directory, but for Go development, it's common to use the Go workspace.&lt;/p&gt;

&lt;p&gt;To set up the workspace, navigate to your home directory and create the following folder structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; ~
&lt;span class="nb"&gt;mkdir&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt; go/src/github.com/_admondtamang
&lt;span class="nb"&gt;cd &lt;/span&gt;go/src/github.com/_admondtamang
git clone https://github.com/admondtamang/[package_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Modify the &lt;code&gt;go.mod&lt;/code&gt; File
&lt;/h3&gt;

&lt;p&gt;For local development, you don’t need to use a tag. Generate the replace directive in your &lt;code&gt;go.mod&lt;/code&gt; file using the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go mod edit &lt;span class="nt"&gt;-replace&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"github.com/mmcdole/gofeed@v1.2.1=/Users/admondtamang/go/src/github.com/_admondtamang/gofeed"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alternatively, you can manually edit the &lt;code&gt;go.mod&lt;/code&gt; file to include the replace directive. The final &lt;code&gt;go.mod&lt;/code&gt; should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="n"&gt;oneaccord&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cc&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;test&lt;/span&gt;

&lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="m"&gt;1.23.0&lt;/span&gt;

&lt;span class="n"&gt;require&lt;/span&gt; &lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;mmcdole&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;gofeed&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="m"&gt;.2.1&lt;/span&gt;

&lt;span class="n"&gt;require&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;PuerkitoBio&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;goquery&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="m"&gt;.8.0&lt;/span&gt; &lt;span class="c"&gt;// indirect&lt;/span&gt;
  &lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;andybalholm&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;cascadia&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="m"&gt;.3.1&lt;/span&gt; &lt;span class="c"&gt;// indirect&lt;/span&gt;
  &lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;iterator&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;go&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="m"&gt;.1.12&lt;/span&gt; &lt;span class="c"&gt;// indirect&lt;/span&gt;
  &lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;mmcdole&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;goxpp&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="m"&gt;.1.1&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;0.20240225020742&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;a0c311522b23&lt;/span&gt; &lt;span class="c"&gt;// indirect&lt;/span&gt;
  &lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;modern&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;go&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;concurrent&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="m"&gt;.0.0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;20180306012644&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;bacd9c7ef1dd&lt;/span&gt; &lt;span class="c"&gt;// indirect&lt;/span&gt;
  &lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;modern&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="k"&gt;go&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;reflect2&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="m"&gt;.0.2&lt;/span&gt; &lt;span class="c"&gt;// indirect&lt;/span&gt;
  &lt;span class="n"&gt;golang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;net&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="m"&gt;.4.0&lt;/span&gt; &lt;span class="c"&gt;// indirect&lt;/span&gt;
  &lt;span class="n"&gt;golang&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;org&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="n"&gt;v0&lt;/span&gt;&lt;span class="m"&gt;.5.0&lt;/span&gt; &lt;span class="c"&gt;// indirect&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;replace&lt;/span&gt; &lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;mmcdole&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;gofeed&lt;/span&gt; &lt;span class="n"&gt;v1&lt;/span&gt;&lt;span class="m"&gt;.2.1&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;Users&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;admondtamang&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;go&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;src&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;_admondtamang&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;gofeed&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for locally development you can modify your changes and it will reflect in your codebase. To get most out of it push changes to Github repo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Publish it to Github
&lt;/h2&gt;

&lt;h3&gt;
  
  
  3. Push Changes to GitHub with a Tag
&lt;/h3&gt;

&lt;p&gt;Once you've made your changes, push them to your forked repository. Create and push a tag:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git tag &lt;span class="s2"&gt;"v0.0.1"&lt;/span&gt;
git push &lt;span class="nt"&gt;--tags&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Update the &lt;code&gt;go.mod&lt;/code&gt; File to Use Your Fork
&lt;/h3&gt;

&lt;p&gt;Replace the dependency in your &lt;code&gt;go.mod&lt;/code&gt; file with the new version from your fork:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go mod edit &lt;span class="nt"&gt;-replace&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"github.com/mmcdole/gofeed@v1.2.1=github.com/admondtamang/gofeed@v0.0.1"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Run &lt;code&gt;go mod tidy&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Finally, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go mod tidy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will clean up your &lt;code&gt;go.mod&lt;/code&gt; file and ensure all dependencies are properly managed.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Using Headless Woo commerce store api v1 in nextjs : issue faced and solutions</title>
      <dc:creator>admond</dc:creator>
      <pubDate>Fri, 13 Sep 2024 00:56:35 +0000</pubDate>
      <link>https://dev.to/admondtamang/using-headless-woo-commerce-store-api-v1-in-nextjs-issue-faced-and-solutions-ieh</link>
      <guid>https://dev.to/admondtamang/using-headless-woo-commerce-store-api-v1-in-nextjs-issue-faced-and-solutions-ieh</guid>
      <description>&lt;p&gt;&lt;strong&gt;Must read note:&lt;/strong&gt;&lt;br&gt;
If you want only solution scroll directly to solution  section&lt;/p&gt;

&lt;p&gt;The blog is about the integrating WooCommerce Store API. Integrating it was very hard because the documentation was lacking. The documents looked simple but didn't have enough information. This blog talks about integrating WooCommerce with a Headless API for Nextjs or React applications, but it can be used for other client-side applications too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Issue: Nonce&lt;/strong&gt;&lt;br&gt;
The first problem was I didn't know what a nonce was. WordPress uses a nonce to make requests more secure. At first, I didn't know what it was. After researching for a while, I found out it was for security purposes. The documentation said the nonce should be retrieved using a PSP code in function.pho. However, they didn’t mention the function.psb part clearly. They only provided the code to generate the token, which frustrated me because there wasn't enough information.&lt;/p&gt;

&lt;p&gt;After deep research on how to generate the nonce, I created an endpoint to generate it. Then, I faced another issue. It wasn’t about the nonce this time; it was about something else. They were using a nonce from the cart list API, and I was shocked to find out that I had created a separate route for something already present in the cart list API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution: nonce was present in cart items api&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0sq6tft7avi65jns7lpv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0sq6tft7avi65jns7lpv.png" alt="Image description" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OR&lt;/p&gt;

&lt;p&gt;Also you can create your custom route by editing in functions.php&lt;br&gt;
which is located in your applied &lt;br&gt;
&lt;code&gt;wp-content/themes/[your_theme]/functions.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
/**********************/
// Api to generate nonce
/**********************/
function get_wc_api_nonce() {
    // Generate WooCommerce nonce
    $nonce = wp_create_nonce( 'wc_store_api' );

    // Return the nonce in a JSON response
    return rest_ensure_response( array( 'nonce' =&amp;gt; $nonce ) );
}

add_action( 'rest_api_init', function() {
    register_rest_route( 'secure/v1', '/wc-nonce', array(
        'methods' =&amp;gt; 'GET',
        'callback' =&amp;gt; 'get_wc_api_nonce',
    ));
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Issue: Cart Token&lt;/strong&gt;&lt;br&gt;
I found a problem with the user trying to add items to the cart multiple times. The user wanted the cart to merge when they logged in. They provided a workflow example, and I noticed a cart token in the response error. I decoded it and found it stored the user ID and some other minor details, but nothing important, so I ignored it.&lt;/p&gt;

&lt;p&gt;After not finding anything helpful, I dug into the source code and implementation. After a long time, I discovered that only two headers were allowed: the cart token and nonce. I realized the cart token was used in previous issues. So, I tried using the JWT token from the response header in our application, and it worked.&lt;/p&gt;

&lt;p&gt;If this had been mentioned in the documentation, it would have been easier for me. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F01vvgw86lebgmdwbwett.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F01vvgw86lebgmdwbwett.png" alt="Image description" width="800" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:  First use add to cart api, get the cart-token from response header and add header Cart-token :YOUR_JWT token, to access this in client side you need to create an serverside endpoint so that you can access the Cart-token as it is not exposed for cors config., then your are go to go&lt;/strong&gt;. Sample endpoint for nextjs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { storeApi } from '@/lib/api';

const CART_TOKEN_HEADER_KEY = 'cart-token';

/**
 * info: this route is used to get the cart data from the store api by appending the cart token to the body
 * @param request
 * @returns
 */
export async function GET(request: Request) {
  const headers = request.headers;

  const savedCartToken = headers.get(CART_TOKEN_HEADER_KEY);

  const res = await storeApi.get('/cart', {
    headers: {
      ...(savedCartToken ? { [CART_TOKEN_HEADER_KEY]: savedCartToken } : {}),
    },
  });

  const responseData = {
    data: res.data,
    cartToken: res.headers[CART_TOKEN_HEADER_KEY],
  };

  return Response.json(responseData);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Docs&lt;br&gt;
&lt;a href="https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/src/StoreApi/docs/cart-tokens.md" rel="noopener noreferrer"&gt;https://github.com/woocommerce/woocommerce/blob/trunk/plugins/woocommerce/src/StoreApi/docs/cart-tokens.md&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Payment Issue&lt;/p&gt;

&lt;p&gt;Coming soon &lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>woocommerce</category>
      <category>headless</category>
      <category>econmerce</category>
    </item>
  </channel>
</rss>
